Python 3 和 Slack 客户端:ssl.SSLCertVerificationError

JFM*_*uis 5 python-3.x slack

我正在尝试在 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')

Run Code Online (Sandbox Code Playgroud)

这是错误:

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)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

小智 10

以下说明对我有用:

import ssl
import certifi
ssl_context = ssl.create_default_context(cafile=certifi.where())
client = slack.WebClient(token=os.environ['SLACK_TOKEN'], ssl=ssl_context)
Run Code Online (Sandbox Code Playgroud)


stu*_*3nt -3

相信您在课堂上使用了不正确的令牌WebClient。尝试在以下位置使用 Bot 用户访问令牌WebClient

slack_token="xoxb-*******-*******-******-******"
client = slack.WebClient(slack_token)
Run Code Online (Sandbox Code Playgroud)

OAuth 令牌有两种类型:

  1. OAuth 访问令牌-开头为 xoxp-
  2. 机器人用户 OAuth 访问令牌-开头为 xoxb-

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

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