When trying to call an API using python Requests I'm getting certificate verify failed: unable to get local issuer certificate error.
I have added the SSL certificate in cacert.pem of certifi. This did not fix the problem.
data = {'foo':'bar'} url = 'hostname' r = requests.post(url, data=data) print(r) The above code only works when I put verify=False in the above code.
I have also extracted SSL certificate of API using the below code and added it to certifi cacert.pem but that too did not work.
import ssl, socket myhostname = 'hostname' myctx = ssl.create_default_context() myctx.check_hostname = False myctx.verify_mode = ssl.CERT_NONE s = myctx.wrap_socket(socket.socket(), server_hostname=myhostname) s.connect((myhostname, 443)) bcert = s.getpeercert(binary_form=True) cert = ssl.DER_cert_to_PEM_cert(bcert) Below is the error that is getting thrown
Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen httplib_response = self._make_request( File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request self._validate_conn(conn) File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 976, in _validate_conn conn.connect() File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connection.py", line 361, in connect self.sock = ssl_wrap_socket( File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\ssl_.py", line 377, in ssl_wrap_socket return context.wrap_socket(sock, server_hostname=server_hostname) File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1040, in _create self.do_handshake() File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
REQUESTS_CA_BUNDLEto point to your certs? I’ve had issues before where I’ve had to explicitly set that value and that fixed cert issues (the default certs with python did not work with the self-signed certificates on the sites I needed to hit).