You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Slack Bolt请求API报SSL证书验证失败错误

问题描述

我正参考官方教程尝试创建基础Slack应用,已完成Slack应用创建流程并生成了所需的全部令牌,经检查令牌配置无误,但运行程序时始终出现如下报错:

Failed to send a request to Slack API server: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>
Failed to send a request to Slack API server: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py", line 1350, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 950, in send
    self.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1424, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1040, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)

我的app.py代码如下:

import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

# Initializes your app with your bot token and socket mode handler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))

# Start your app
if __name__ == "__main__":
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
问题原因

该报错和Slack令牌配置无关,是本地Python环境的SSL根证书缺失导致的。Python向Slack API发起HTTPS请求时,无法验证服务器返回证书的签发机构合法性,因此抛出证书验证失败错误。从报错路径判断,运行环境为Mac系统,使用的是从python.org官网下载安装的Python 3.8版本,该版本默认不会自动安装配置公共根证书库,是这类问题的高发场景。

解决方法

按优先级选择对应方案处理即可:

  • 优先修复本地证书配置(推荐,无安全风险)
    打开终端执行Python自带的证书安装脚本:
    cd "/Applications/Python 3.8/"
    sudo ./Install\ Certificates.command
    
    执行完成后重新运行Slack应用即可正常连接。如果安装的是其他版本的Python,将路径中的3.8替换为对应版本号即可。
  • 临时绕过验证(仅适用于本地调试,生产环境禁止使用,存在安全风险)
    若暂时无法修复证书配置,可通过代码临时关闭SSL证书验证,修改后的代码如下:
    import os
    import ssl
    # 临时关闭SSL证书校验
    ssl._create_default_https_context = ssl._create_unverified_context
    
    from slack_bolt import App
    from slack_bolt.adapter.socket_mode import SocketModeHandler
    
    app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
    
    if __name__ == "__main__":
        SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
    
    该方案会让当前程序所有HTTPS请求跳过证书校验,存在中间人攻击风险,仅可用于本地临时调试。
  • 内网环境特殊处理
    如果在公司内网环境运行程序,先确认公司是否对HTTPS流量做了代理解密,如果存在该情况,需要将公司内网的根证书导入到Python的证书信任库中,否则即使安装了默认根证书也会出现相同报错。

内容的提问来源于stack exchange,提问作者AJ.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 10:12:21