Python -Rest API to fetch the bearer – Requests module

Question:

I have an API, where I need to get the bearer token. When I use ‘Postman’ application, I get the bearer token correctly. I have written below python code for the same but I get below errors. Please help. I need to send username and password in the body as a form data.

import requests

url = "https://322.286.24.01/ach/ach_api/login"

payload={'username': 'test',
'password': 'test12'}

response = requests.post( url,data=payload)

print(response.text)

ERROR:

Traceback (most recent call last):
  File "/tmp/test.py", line 13, in <module>
    response = requests.post(url,data=payload)
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='322.286.24.01', port=443): Max retries exceeded with url: /ach/ach_api/login (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1123)')))

Asked By: stacktesting

||

Answers:

The error is because requests is trying to check for the ca cert, try the following:

response = requests.post(url, data=payload, verify=False)

Or, if you have a ca.crt somewhere, usually pem format, you can try:

response = requests.post(url, data=payload, verify='/path/to/pem')

Also the IP address looks funny, although since the client connects I suspect you just changed that to anonymise your post?

Answered By: james hedley
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.