基于OpenAI的AfricasTalking SMS助手遇SSL版本错误求助
解决AfricasTalking API调用的SSL版本错误问题
错误原因分析
WRONG_VERSION_NUMBER错误核心原因是SSL/TLS版本不兼容:
- 本地环境SSL库版本与AfricasTalking沙箱API要求的版本不匹配
- 网络代理强制使用了过时的SSL协议
- 请求URL与端口配置冲突
具体解决方法
1. 强制指定兼容的TLS版本
修改africastalking_api.py中的response_to_sms函数,通过自定义适配器强制使用TLS 1.2或1.3:
import requests from requests.adapters import HTTPAdapter from urllib3.poolmanager import PoolManager import ssl class TLSAdapter(HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=False): self.poolmanager = PoolManager( num_pools=connections, maxsize=maxsize, block=block, ssl_version=ssl.PROTOCOL_TLSv1_2 # 也可尝试ssl.PROTOCOL_TLSv1_3 ) def response_to_sms(recipient, message): session = requests.Session() session.mount('https://', TLSAdapter()) url = "https://api.sandbox.africastalking.com/version1/messaging" headers = { "ApiKey": "你的API密钥", "Content-Type": "application/x-www-form-urlencoded" } data = { "username": "sandbox", # 沙箱环境固定用sandbox "to": recipient, "message": message } response = session.post(url, headers=headers, data=data) return response.json()
2. 排查网络代理
如果使用了代理,临时关闭代理测试请求是否正常;若必须使用代理,确保代理未强制启用SSLv3等过时协议,同时在requests请求中配置代理时同步指定TLS版本。
3. 验证URL与端口正确性
- 沙箱环境正确URL为
https://api.sandbox.africastalking.com/version1/messaging,默认端口443(HTTPS标准端口) - AfricasTalking沙箱API仅支持HTTPS,请勿切换为HTTP
4. 更新本地SSL依赖
在开发环境执行以下命令更新SSL相关库:
pip install --upgrade pip pip install --upgrade pyopenssl cryptography
额外排查步骤
- 确认API密钥和沙箱用户名(固定为
sandbox)无误 - 用curl直接测试API,排除代码逻辑问题:
curl -X POST https://api.sandbox.africastalking.com/version1/messaging \ -H "ApiKey: 你的API密钥" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=sandbox&to=测试号码&message=测试内容"
内容的提问来源于stack exchange,提问作者Alex ombongi
相关产品推荐
相关产品推荐

