HTTR GET请求失败:SSL对等证书验证不通过问题求助
解决调用Shoplightspeed API时的SSL证书不信任错误
问题重现
调用Shoplightspeed API的GET请求在R、Python、cURL中均返回以下错误:
Error in curl::curl_fetch_memory(url, handle = handle) :
SSL peer certificate or SSH remote key was not OK: [api.shoplightspeed.com] schannel: SEC_E_UNTRUSTED_ROOT (0x80090325) - The certificate chain was issued by an authority that is not trusted.
测试用R代码:
library(httr) library(jsonlite) ecom_key <- 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy' ecom_sec <- 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' url <- paste0('https://',ecom_key,':',ecom_sec,'@api.shoplightspeed.com/en/') account <- GET(paste0(url,'account.json'))
已尝试更新cURL、使用openssl库,但问题未解决;Postman中请求可正常执行。
解决方案
1. 检查并修复系统根证书信任
Postman能正常工作,大概率是本地系统的受信任根证书颁发机构中缺少API证书对应的根CA:
- Windows:打开
certmgr.msc,导航到「受信任的根证书颁发机构」→「证书」,确认是否包含API证书链的根CA(可通过浏览器访问https://api.shoplightspeed.com,查看证书详情导出根证书) - 如果缺失,手动导入导出的根证书(右键证书→「安装证书」→选择「本地计算机」→「将所有证书放入下列存储」→选择「受信任的根证书颁发机构」)
- Windows系统可尝试自动更新根证书:打开命令提示符执行
certutil -syncWithWU
2. 在脚本中指定信任证书(或临时跳过验证,仅测试用)
R(httr库)
library(httr) # 方法1:临时关闭SSL验证(仅测试,生产环境禁止) account <- GET(paste0(url,'account.json'), config(ssl_verifypeer = FALSE)) # 方法2:指定根证书路径 account <- GET(paste0(url,'account.json'), config(ssl_ca_cert = "C:/path/to/root_cert.pem"))
cURL
# 指定根证书 curl --cacert /path/to/root_cert.pem https://<your_key>:<your_secret>@api.shoplightspeed.com/en/account.json # 临时关闭验证(测试用) curl -k https://<your_key>:<your_secret>@api.shoplightspeed.com/en/account.json
Python(requests库)
import requests # 临时关闭验证(测试用) response = requests.get(url, verify=False) # 指定根证书 response = requests.get(url, verify='/path/to/root_cert.pem')
3. 排查代理干扰
如果本地使用了代理服务器,代理可能会替换SSL证书导致验证失败:
- 尝试关闭代理后重新测试请求
- 若必须使用代理,需在脚本中配置代理的证书,或确保代理证书已加入系统信任列表
4. 检查安全软件拦截
部分杀毒软件、防火墙可能会拦截SSL证书验证,可临时关闭安全软件测试,若问题解决,需将API域名加入安全软件的信任列表。
内容的提问来源于stack exchange,提问作者CodingCrunch
相关产品推荐
相关产品推荐

