Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

certificate - 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.

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 is password protected.

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:UsersmeDesktopest.py", line 65, in <module>
r = requests.post(url, params=payload, headers=headers, data=payload, verify=cafile)
File "C:Python33libsite-packages
equestsapi.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "C:Python33libsite-packages
equestsapi.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "C:Python33libsite-packages
equestssessions.py", line 346, in request
resp = self.send(prep, **send_kwargs)
File "C:Python33libsite-packages
equestssessions.py", line 449, in send
r = adapter.send(request, **kwargs)
File "C:Python33libsite-packages
equestsadapters.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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had this same problem. The verify parameter refers to the server's certificate. You want the cert parameter to specify your client certificate.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...