Home > Mobile >  Python API call using requests throws Max retries exceeded with url error
Python API call using requests throws Max retries exceeded with url error

Time:12-31

I'm trying to access an https API using Python requests. I was given a cert and a passphrase to access this.

I'm getting

HTTPSConnectionPool(host='abc.coom', port=443): Max retries exceeded with url: //AIMWebService//api//Accounts?AppID=xyz&Safe=123&Object=obj  (Caused by SSLError(SSLError(9, '[SSL] PEM lib (_ssl.c:4024)')))

Earlier, I accessed this API and got results successfully in Postman by adding the certificates in Setting->Certificates along with the passphrase.

import json
import requests

clientCrt = "C:\\abc\\abc-Certs\\cert-TEST.pfx"

#clientKey = "Passphrase"
url = "https://abc.coom/AIMWebService/api/Accounts?AppID=xyz&Safe=123&Object=obj"
certServer = "C:\\Users\\MyName\\Anaconda3\\Lib\\site-packages\\certifi"
headers = {'content-type': 'application/json'}
#r = requests.get(url, verify=False, headers=headers, cert=(clientCrt, clientKey))
r = requests.get(url, verify=False, headers=headers, cert=clientCrt)
#r = requests.get(url, verify=certServer, headers=headers, cert=clientCrt)
#r = requests.get(url, verify=False, headers=headers)
print(r.status_code)
print(r.json())

As you can see, I tried multiple options to access it. By using verify=False, I thought I did not have a need to use the passphrase at all.

Can someone help me to overcome this?

CodePudding user response:

requests expects a certificate with the PEM format your .pfx file is in the PKCS#12 format.

You can either change your certificate format : Converting pfx to pem using openssl.

Or you can use this library that adds PKCS#12 support to requests.

  • Related