简体   繁体   中英

Python -Rest API to fetch the bearer - Requests module

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)')))

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM