10

I'm trying to access Slack in python 3.8 but i'm unable to pass the first step. Here is my code :

import slack

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

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

print('hello')

and here is the error:

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 config:

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
David Z
  • 128,184
  • 27
  • 255
  • 279
JFMarquis
  • 127
  • 1
  • 1
  • 4
  • 2
    Side note, if that's your real slack access token, you should invalidate it and get a new one. – Erty Seidohl Jan 20 '20 at 04:01
  • How about `pip install certifi` ? – Mike Doe Jan 20 '20 at 07:12
  • already installed : see venv config – JFMarquis Jan 21 '20 at 12:05
  • 1
    Does this answer your question? [Scraping: SSL: CERTIFICATE\_VERIFY\_FAILED error for http://en.wikipedia.org](https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org) – David Z Aug 09 '20 at 06:05
  • 1
    This seems to be the same issue as several other questions: https://stackoverflow.com/questions/50236117, https://stackoverflow.com/questions/52805115, https://stackoverflow.com/questions/59411362, and probably others. – David Z Aug 09 '20 at 06:07

3 Answers3

9

Following instruction worked for me:

import ssl
import certifi
ssl_context = ssl.create_default_context(cafile=certifi.where())
client = slack.WebClient(token=os.environ['SLACK_TOKEN'], ssl=ssl_context)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

Finally i found a workaround by using this code

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)
JFMarquis
  • 127
  • 1
  • 1
  • 4
  • Are you able to access it though `OAuth Access Token i.e. xoxp-` token ? – stud3nt Jan 20 '20 at 12:00
  • 2
    I wouldn't really recommend this because it turns off certificate validation entirely and makes your code susceptible to man-in-the-middle attacks. – David Z Aug 09 '20 at 06:08
-2

Believe you are using incorrect token in the WebClient class. Try using Bot user access token in WebClient:

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

There are two types OAuth tokens:

  1. OAuth Access Token - starts with xoxp-
  2. Bot User OAuth Access Token - starts with xoxb-

Note: you can access the tokens of your app/bot from url - https://api.slack.com/apps/{yourappid}/oauth?.

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

stud3nt
  • 2,056
  • 1
  • 12
  • 21