I am trying to pull an active list of users for our slack workspace to use in our IT Department. The goal is to have a webapp that we can see everyone in our applications. I get the following error when trying to connect to the API by using the token from the slack bot I had to make:
macOS/Linux
To get a list/dictionary of active users
Traceback (most recent call last):
File "/Users/johnmichaelmizerany/Desktop/UserDashboard/slack.py", line 15, in <module>
result = client.users_list()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/client.py", line 2249, in users_list
return self.api_call("users.list", http_verb="GET", params=kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 135, in api_call
return self._sync_send(api_url=api_url, req_args=req_args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 171, in _sync_send
return self._urllib_api_call(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 284, in _urllib_api_call
response = self._perform_urllib_http_request(url=url, args=request_args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 437, in _perform_urllib_http_request
raise err
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 411, in _perform_urllib_http_request
resp = urlopen( # skipcq: BAN-B310
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1389, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1349, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)>
Hi @jmmiz 馃憢馃徎 Thanks for the actual result stack trace!
Just to confirm, have you set your SLACK_BOT_TOKEN environment variable, for example:
import os
from slack_sdk import WebClient
slack_token = os.environ["SLACK_BOT_TOKEN"]
client = WebClient(token=slack_token)
result = client.users_list
If so, can you provide a code sample to recreate your issue? I imagine it's related to the CERTIFICATE_VERIFY_FAILED error in your stack trace, so seeing how you've set up the app will help!
@mwbrooks here is my code below
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
users_store = {}
try:
result = client.users_list()
save_users(result["members"])
except SlackApiError as e:
logger.error("Error creating conversation: {}".format(e))
def save_users(users_array):
for user in users_array:
user_id = user["id"]
users_store[user_id] = user`
#I am replacing "SLACK_BOT_TOKEN" with the actual token provided from slack
@jmmiz I'm not 100% sure if this helps you out but what happens if you run the following script after pip install -U slack-sdk certifi?
# pip install -U slack-sdk certifi
import certifi, logging, os, ssl
from slack_sdk import WebClient
logging.basicConfig(level=logging.DEBUG)
ssl_context = ssl.create_default_context(cafile=certifi.where())
client = WebClient(ssl=ssl_context)
client.api_test()
This is what I get when running that script @seratch
johnmichaelmizerany@Johns-MBP` UserDashboard % python3 slack.py
DEBUG:slack_sdk.web.base_client:Sending a request - url: https://www.slack.com/api/api.test, query_params: {}, body_params: {}, files: {}, json_body: {}, headers: {'Content-Type': 'application/json;charset=utf-8', 'User-Agent': 'Python/3.9.5 slackclient/3.5.1 Darwin/20.3.0'}
DEBUG:slack_sdk.web.slack_response:Received the following response - status: 200, headers: {'date': 'Sat, 08 May 2021 04:09:03 GMT', 'server': 'Apache', 'access-control-expose-headers': 'x-slack-req-id, retry-after', 'access-control-allow-headers': 'slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags', 'strict-transport-security': 'max-age=31536000; includeSubDomains; preload', 'access-control-allow-origin': '*', 'x-slack-backend': 'r', 'x-xss-protection': '0', 'x-slack-req-id': '6abedc6e6221291f599093fdfb5ae69a', 'vary': 'Accept-Encoding', 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer', 'content-type': 'application/json; charset=utf-8', 'x-envoy-upstream-service-time': '4', 'x-backend': 'main_normal main_canary_with_overflow main_control_with_overflow', 'x-server': 'slack-www-hhvm-main-iad-wddn', 'x-via': 'envoy-www-iad-azar, haproxy-edge-iad-bvfs', 'x-slack-shared-secret-outcome': 'shared-secret', 'via': 'envoy-www-iad-azar', 'connection': 'close', 'transfer-encoding': 'chunked'}, body: {'ok': True, 'args': {}}
@jmmiz It looks successful! Can you try the same code for the app that you had the CERTIFICATE_VERIFY_FAILED error?
Just tried it and I get the same error ): @seratch
My only guess is that the token is not correct but that is the only slack bot token I was given?
Traceback (most recent call last):
File "/Users/johnmichaelmizerany/Desktop/UserDashboard/slack.py", line 15, in <module>
result = client.users_list()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/client.py", line 2249, in users_list
return self.api_call("users.list", http_verb="GET", params=kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 135, in api_call
return self._sync_send(api_url=api_url, req_args=req_args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 171, in _sync_send
return self._urllib_api_call(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 284, in _urllib_api_call
response = self._perform_urllib_http_request(url=url, args=request_args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 437, in _perform_urllib_http_request
raise err
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 411, in _perform_urllib_http_request
resp = urlopen( # skipcq: BAN-B310
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1389, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1349, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)>
johnmichaelmizerany@Johns-MBP UserDashboard %
@jmmiz The validity of your token is not related to the URLError, which means a failure while connecting to the Slack API endpoint. If your app successfully connects to Slack and gets an error response for the invalid token, you will see the response body: {"ok": false, "error": "invalid_auth"}.
Just in case, can you verify if you've modified your code as below? As the above code calling api.test API succeeds, doing this way should be successful as well.
import certifi, os, ssl
from slack_sdk import WebClient
ssl_context = ssl.create_default_context(cafile=certifi.where())
client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"), ssl=ssl_context)
Yes! I tried again and this is what I was given @seratch :
Traceback (most recent call last):
File "/Users/johnmichaelmizerany/Desktop/UserDashboard/slack.py", line 13, in <module>
result = client.users_list()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/client.py", line 2249, in users_list
return self.api_call("users.list", http_verb="GET", params=kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 135, in api_call
return self._sync_send(api_url=api_url, req_args=req_args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 171, in _sync_send
return self._urllib_api_call(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/base_client.py", line 303, in _urllib_api_call
return SlackResponse(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/slack_sdk/web/slack_response.py", line 201, in validate
raise e.SlackApiError(message=msg, response=self)
slack_sdk.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'not_authed'}
johnmichaelmizerany@Johns-MBP UserDashboard %
{'ok': False, 'error': 'not_authed'}
@jmmiz Congrats! This means your WebClient instance does not provide any token (perhaps, the env variable SLACK_BOT_TOKEN is missing or unloaded for some reason). If you pass a valid token either in the WebClient constructor or as token in method arguments, your code should work for you.
It seems that everything about the original question is clear now. Would you mind closing this issue?
Yes! Thank you for your help @seratch