I am trying to use requests module of Python3 to make an API call. I am using credentials that work when tried on same service from their web page. So credentials are verified.
Code that I am using:
#!/local/usr/bin/python3
import requests
url = "https://some.url.com/fol/something"
payload = "{'userName': \"user-name\", 'instanceName': \"instance-name\", 'password': \"user-password\", 'usersDomain': \"DOMAIN\"}"
headers = {'accept': 'application/json','content-type': 'application/json'}
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
print(response)
Problem:
If I remove verify=False
from line 7 I get ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
.
With verify=False
(as in code above) I get following warning and response code is 401.
InsecureRequestWarning: Unverified HTTPS request is being made to host 'wifitracker.chartercom.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning,
<Response [401]>
What I have tried:
- Tried using same code from different Linux servers with different Python and requests module version.
- Tried
ping {host}
andnc -vz {host} 443
and both succeed. The API endpoint and Linux server from where I am trying to connect are both on the same network domain. - Tried adding
verify="/etc/ssl/certs/ca-bundle.crt"
andverify="/etc/ssl/certs/ca-bundle.trust.crt"
as well asverify="/etc/ssl/certs"
andverify="/etc/ssl/certs/"
. All these options don't cause any error/warning but the response code is still 401. - Used python module
certifi
and the found the installed certification authority budle and used it in theveryify=
but I getssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
- Following suggestion by Steffen I tried
openssl s_client -connect some.url.com:443
and result is (hope it's not too much):
USR--> openssl s_client -connect some.url.com:443
CONNECTED(00000003)
depth=2 C = <My Country Code>, O = <My Organization Name>, OU = <My Organization Name> Internal Tr<My Country Code>t Network, OU = (c) 2017 <My Organization Name> - For authorized <My Country Code>e only, CN = <My Organization Name> Root Certification Authority
verify error:num=19:self signed certificate in certificate chain
---
Certificate chain
0 s:/C=<My Country Code>/ST=<My State>/L=<My City>/O=<My Organization Name>/CN=some.url.com
i:/DC=com/DC=url/DC=corp/CN=<My Organization Name> Issuing CA1
1 s:/DC=com/DC=url/DC=corp/CN=<My Organization Name> Issuing CA1
i:/C=<My Country Code>/O=<My Organization Name>/OU=<My Organization Name> Internal Tr<My Country Code>t Network/OU=(c) 2017 <My Organization Name> - For authorized <My Country Code>e only/CN=<My Organization Name> Root Certification Authority
2 s:/C=<My Country Code>/O=<My Organization Name>/OU=<My Organization Name> Internal Tr<My Country Code>t Network/OU=(c) 2017 <My Organization Name> - For authorized <My Country Code>e only/CN=<My Organization Name> Root Certification Authority
i:/C=<My Country Code>/O=<My Organization Name>/OU=<My Organization Name> Internal Tr<My Country Code>t Network/OU=(c) 2017 <My Organization Name> - For authorized <My Country Code>e only/CN=<My Organization Name> Root Certification Authority
---
Server certificate
-----BEGIN CERTIFICATE-----
<CHUNK OF TEXT>
-----END CERTIFICATE-----
subject=/C=<My Country Code>/ST=<My State>/L=<My City>/O=<My Organization Name>/CN=some.url.com
issuer=/DC=com/DC=url/DC=corp/CN=<My Organization Name> Issuing CA1
---
No client certificate CA names sent
Peer signing digest: SHA256
Server Temp Key: ECDH, P-256, 256 bits
---
SSL handshake has read 6834 bytes and written 433 bytes
---
New, TLSv1/SSLv3, Cipher is <SOME-ALHPA-NUMERIC-CHARACTERS>
Server public key is 2048 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
Protocol : TLSv1.2
Cipher : <SOME-ALHPA-NUMERIC-CHARACTERS>
Session-ID: <SOMELONGALHPANUMERIC64CHARACTERS>
Session-ID-ctx:
Master-Key: <SOMELONGALHPANUMERIC96CHARACTERS>
Key-Arg : None
PSK identity: None
PSK identity hint: None
SRP username: None
Start Time: <epoch_time>
Timeout : 300 (sec)
Verify return code: 19 (self signed certificate in certificate chain)
---
I tried finding options (from here on SO and other resources) and think I have tried those but obviously I am still missing something.
What else can I try to make a successful API call (status code 200)? Could it be that I need ca cert from the API provider? (It's a security related application)