28

我正在用 python 中的请求调用 REST API,到目前为止,当我设置verify=False.

现在,我必须使用需要导入以进行身份​​验证的客户端证书,并且每次使用cert (.pfx). cert.pfx受密码保护时都会收到此错误。

r = requests.post(url, params=payload, headers=headers, 
                  data=payload, verify='cert.pfx')

这是我得到的错误:

Traceback (most recent call last):
File "C:\Users\me\Desktop\test.py", line 65, in <module>
r = requests.post(url, params=payload, headers=headers, data=payload, verify=cafile)
File "C:\Python33\lib\site-packages\requests\api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "C:\Python33\lib\site-packages\requests\api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 346, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python33\lib\site-packages\requests\sessions.py", line 449, in send
r = adapter.send(request, **kwargs)
File "C:\Python33\lib\site-packages\requests\adapters.py", line 322, in send
raise SSLError(e)
requests.exceptions.SSLError: unknown error (_ssl.c:2158)

我也尝试过 openssl 来获取.pem和密钥,但使用.pem和获取SSL: CERTIFICATE_VERIFY_FAILED

有人可以指导我如何导入证书以及将其放置在哪里?我尝试搜索但仍然面临同样的问题。

4

2 回答 2

66

我有同样的问题。该verify参数是指服务器的证书。您希望该cert参数指定您的客户端证书。

import requests
cert_file_path = "cert.pem"
key_file_path = "key.pem"

url = "https://example.com/resource"
params = {"param_1": "value_1", "param_2": "value_2"}
cert = (cert_file_path, key_file_path)
r = requests.get(url, params=params, cert=cert)
于 2013-07-11T21:46:10.777 回答
5

我遇到了同样的问题,为了解决这个问题,我知道我们必须将 RootCA 连同证书及其密钥一起发送,如下所示,

response = requests.post(url, data=your_data, cert=('path_client_certificate_file', 'path_certificate_key_file'), verify='path_rootCA')
于 2021-02-24T10:00:41.810 回答