简体   繁体   English

Python 请求 - 客户端证书的 SSL 错误

[英]Python Requests - SSL error for client side cert

I'm calling a REST API with requests in python and so far have been successful when I set verify=False .我正在调用 REST API 并在 python 中提出请求,到目前为止,当我设置verify=False时已经成功。

Now, I have to use client side cert that I need to import for authentication and I'm getting this error everytime I'm using the cert (.pfx). cert.pfx现在,我必须使用需要导入的客户端证书进行身份验证,每次使用cert (.pfx). cert.pfx cert (.pfx). cert.pfx is password protected. cert (.pfx). cert.pfx受密码保护。

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

This is the error I'm getting:这是我得到的错误:

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)

I've also tried openssl to get .pem and key but with .pem and getting SSL: CERTIFICATE_VERIFY_FAILED我也尝试过 openssl 来获取.pem和密钥,但使用.pem并获取SSL: CERTIFICATE_VERIFY_FAILED

Can someone please direct me on how to import the certs and where to place it?有人可以指导我如何导入证书以及将其放置在哪里? I tried searching but still faced with the same issue.我尝试搜索但仍然面临同样的问题。

I had this same problem.我有同样的问题。 The verify parameter refers to the server's certificate. verify参数指的是服务器的证书。 You want the cert parameter to specify your client certificate.您希望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)

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

response = requests.post(url, data=your_data, cert=('path_client_certificate_file', 'path_certificate_key_file'), verify='path_rootCA')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM