slack chat.postMessage API端点不允许授权标头

Gop*_*ngh 4 javascript slack-api fetch-api

我在浏览器中运行这段代码

<html>

    <script type="module">
        console.log("working");

        var url = "https://slack.com/api/chat.postMessage";
        var auth_token = "xoxb-2B"; //Your Bot's auth token
        var body = {channel: "ses", text: "testing app"}

        async function postData(url = '', data = {}) {
            // Default options are marked with *
            const response = await fetch(url, {
                method: 'POST', // *GET, POST, PUT, DELETE, etc.
                headers: {
                    "Authorization": "Bearer " + auth_token,
                    "Content-Type" : "application/json"
                },
                body: JSON.stringify(data) // body data type must match "Content-Type" header
            });
            return response.json(); // parses JSON response into native JavaScript objects
        }

        postData('https://slack.com/api/chat.postMessage', body)
        .then(data => {
            console.log(data); // JSON data parsed by `data.json()` call
        });
    </script>
</html>
Run Code Online (Sandbox Code Playgroud)

我越来越

从源“http://127.0.0.1:5500”获取“https://slack.com/api/chat.postMessage”的访问已被 CORS 策略阻止:访问控制不允许请求标头字段授权- 预检响应中允许标头。

我不明白,我需要以某种方式指定不记名令牌,即使在文档中它说将其放入授权标头中,为什么他们不允许它?

Rub*_*epo 5

\n

我不明白,我需要以某种方式指定不记名令牌,即使文档中说将其放入授权标头中,为什么他们不允许这样做?

\n
\n

这是一个不同的问题,与 Bearer 令牌根本无关。\n从您收到的错误来看,这意味着您用于获取 Slack API 的源不受信任 ( http:// 127.0.0.1:5500),您无法从浏览器执行任何操作,因为这是来自定义授权来源的服务器的策略。(在此处了解有关 CORS 的更多信息)\n由于我认为 Slack 不支持此功能,因此您需要从服务器获取 Slack API。

\n

解决此问题的一种方法是公开后端 API,例如:

\n
\n\n\n\n\n\n\n\n
将消息发布到 Slack \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\ xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\ xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0Run in Fusebit
\n
\n
router.post(\'/api/tenant/:tenantId/test\', async (ctx) => {\n  // Create a Slack client pre-configured with credentials necessary to communicate with your tenant\'s Slack workspace.\n  // For the Slack SDK documentation, see https://slack.dev/node-slack-sdk/web-api.\n  const slackClient = await integration.tenant.getSdkByTenant(ctx, connectorName, ctx.params.tenantId);\n\n  // Get the Slack user ID associated with your tenant\n  const slackUserId = slackClient.fusebit.credentials.authed_user.id;\n\n  // Send a Direct Message to the Slack user\n  const result = await slackClient.chat.postMessage({\n    text: \'Hello world!\',\n    channel: slackUserId,\n  });\n\n  console.log(\'message response\', result.message);\n  ctx.body = { message: `Successfully sent a message to Slack user ${slackUserId}!` };\n});\n\n
Run Code Online (Sandbox Code Playgroud)\n