【问题标题】:Using Slack API from Python在 Python 中使用 Slack API
【发布时间】:2019-04-05 17:17:32
【问题描述】:

我正在按照以下教程 Slack API tutorial 在 slack 上发布消息。我已经创建了 Slack 应用程序,并且能够将它与 Incoming Webhooks 一起使用,现在我想使用来自 python 的 API。所以我尝试了:

import os
from slackclient import SlackClient
slack_token = 'xoxp-long-sequence-of-numbers'
sc = SlackClient(slack_token)

sc.api_call(
  "chat.postMessage",
  channel="my_test_channel",
  text="Hello from Python! :tada:"
)

我收到以下错误消息:

    {'error': 'missing_scope',
 'headers': {'Access-Control-Allow-Headers': 'slack-route, x-slack-version-ts',
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Expose-Headers': 'x-slack-req-id',
  'Cache-Control': 'private, no-cache, no-store, must-revalidate',
  'Connection': 'keep-alive',
  'Content-Encoding': 'gzip',
  'Content-Length': '105',
  'Content-Type': 'application/json; charset=utf-8',
  'Date': 'Fri, 05 Apr 2019 17:07:06 GMT',
  'Expires': 'Mon, 26 Jul 1997 05:00:00 GMT',
  'Pragma': 'no-cache',
  'Referrer-Policy': 'no-referrer',
  'Server': 'Apache',
  'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload',
  'Vary': 'Accept-Encoding',
  'Via': '1.1 6529456e34a07353ab1987432f192696.cloudfront.net (CloudFront)',
  'X-Accepted-OAuth-Scopes': 'chat:write:user',
  'X-Amz-Cf-Id': 'wunHQVQpZicf-ynJO2u_n6CAQEGlYBH67ysu0fP1mfTEt86rRiAbrw==',
  'X-Cache': 'Miss from cloudfront',
  'X-Content-Type-Options': 'nosniff',
  'X-OAuth-Scopes': 'identify,incoming-webhook',
  'X-Slack-Req-Id': '990760fb-b110-43a1-86cb-fd4f8e2e34fa',
  'X-Via': 'haproxy-www-km3w',
  'X-XSS-Protection': '0'},
 'needed': 'chat:write:user',
 'ok': False,
 'provided': 'identify,incoming-webhook'}

我的频道名称实际上是:my_test_channel。可能我需要使用不同的名称,但我怎样才能找到它?

更新

所以我找到了编码的频道 ID 并将我的脚本更改为:

import os
    from slackclient import SlackClient
    slack_token = 'xoxp-long-sequence-of-numbers'
    sc = SlackClient(slack_token)

    sc.api_call(
      "chat.postMessage",
      channel="CHXXXXXXX",
      text="Hello from Python! :tada:"
    )

但错误返回仍然是missing scope

【问题讨论】:

  • my_test_channel 是私有的还是公有的? (您的 slack 或主题标签旁边是否有锁)根据我的经验,slack python api 无法与私人频道交谈
  • @RichardStoeffel。它是公开的,没有锁。有标签#my_test_channel
  • 如何获得自己频道的 slack-token ?

标签: python slack-api


【解决方案1】:

根据错误('needed': 'chat:write:user')看起来它缺少用户选项:

    sc.api_call(
      "chat.postMessage",
      channel="my_test_channel",
      text="Hello from Python! :tada:"
      user="U0XXXXXXX"
    )

【讨论】:

  • 我以用户身份尝试了自己,但返回了同样的错误missing_scope
  • 添加用户时是否出现错误提示?它应该说明缺少哪个范围。
  • 该错误与主帖中的错误相同,只是时间戳不同
  • 好的,你好像没有权限,需要的范围是'chat:write:user',错误提示你丢失了。
  • 说得通,我为我的应用程序创建了机器人,并为它获得了新的 slack_token。令牌以xoxb-.... 开头,一切正常。
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2018-08-20
  • 2018-07-22
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多