【问题标题】:Python 3 & Slack Client : ssl.SSLCertVerificationErrorPython 3 和 Slack 客户端:ssl.SSLCertVerificationError
【发布时间】:2020-05-05 13:56:12
【问题描述】:

我正在尝试在 python 3.8 中访问 Slack,但我无法通过第一步。 这是我的代码:

import slack

slack_token="xoxp-*******-*******-*******-*******"
client = slack.WebClient(slack_token)

client.chat_postMessage(
    channel="XXXXXXXXXX",
    text="Hello from your app! :tada:"
)

print('hello')

这是错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/aiohttp/connector.py", line 936, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1046, in create_connection
    transport, protocol = await self._create_connection_transport(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 1076, in _create_connection_transport
    await waiter
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 529, in data_received
    ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/sslproto.py", line 189, in feed_ssldata
    self._sslobj.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 944, in do_handshake
    self._sslobj.do_handshake()
  ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)

venv 配置:

pip3 freeze
aiohttp==3.6.2
async-timeout==3.0.1
attrs==19.3.0
certifi==2019.11.28
chardet==3.0.4
idna==2.8
multidict==4.7.4
pipenv==2018.11.26
slack==0.0.2
slackclient==2.5.0
virtualenv==16.7.9
virtualenv-clone==0.5.3
yarl==1.4.2

【问题讨论】:

标签: python-3.x slack


【解决方案1】:

最后我找到了使用此代码的解决方法

ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE

sc = slack.WebClient(slack_token,ssl=ssl_context)

【讨论】:

  • 你可以通过OAuth Access Token i.e. xoxp-令牌访问它吗?
  • 我真的不建议这样做,因为它会完全关闭证书验证,并使您的代码容易受到中间人攻击。
【解决方案2】:

相信您在 WebClient 类中使用了不正确的令牌。 尝试在 WebClient 中使用 Bot 用户访问令牌:

slack_token="xoxb-*******-*******-******-******"
client = slack.WebClient(slack_token)

有两种类型的 OAuth 令牌:

  1. OAuth 访问令牌 - xoxp- 开头
  2. Bot 用户 OAuth 访问令牌 - xoxb- 开头

注意:您可以从 url - https://api.slack.com/apps/{yourappid}/oauth? 访问您的应用程序/机器人的令牌。

Slack OAuth 参考 - https://api.slack.com/docs/oauth

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2011-03-15
    相关资源
    最近更新 更多