我的设置是 Mac OS 10.13.6(已更新)、python 2.7.15,并且使用 VPN 进行连接(我需要安装 VPN 证书来解密 HTTPs 流量)。 问题是我无法连接到 slack API
>>> import slackclient
>>> client = slackclient.SlackClient(myToken)
>>> client.server.rtm_connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/slackclient/server.py", line 131, in rtm_connect
reply = self.api_requester.do(self.token, connect_method, timeout=timeout, post_data=kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/slackclient/slackrequest.py", line 104, in do
proxies=self.proxies
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 512, in request
resp = self.send(prep, **send_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 622, in send
r = adapter.send(request, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/adapters.py", line 511, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='slack.com', port=443): Max retries exceeded with url: /api/rtm.start (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)'),))
版本有:
Python 2.7.15
certifi==2018.8.24
chardet==3.0.4
idna==2.7
jenkinsapi==0.3.6
pytz==2018.5
requests==2.19.1
setproctitle==1.1.10
six==1.11.0
slackclient==1.2.1
urllib3==1.23
websocket-client==0.51.0
我发现问题了。 由于某种原因,python不使用我的钥匙链中的证书,而是使用/Library/Frameworks/Python.framework/Versions/2.7/etc/openssl/cert.pem下的openssl中的证书
我已将证书添加到文件底部,问题就消失了
要查找证书的位置,请启动 python 并运行以下命令:
>>> import certifi
>>> certifi.where()
另一个解决方案, 在您的钥匙串访问中删除 slack.com 令牌
另一种解决方案,在 slackclient 调用 websocket-client 时添加 No cert 选项 在文件“c:/Python27/Lib/site-packages/slackclient/server.py”中
添加第 180 行,sslopt={'cert_reqs':0}
line 176: self.websocket = create_connection(ws_url,
line 177: http_proxy_host=proxy_host,
line 178: http_proxy_port=proxy_port,
line 179: http_proxy_auth=proxy_auth,
line 180: sslopt={'cert_reqs':0})