'400: Bad Request' when I try to send a POST request to the Auth API using Python.
I am trying to replicate a Postman request using Python. I use the Request module, specifically the Request and Session objects to create a prepared request and to send the request to the authorization API, which generates the Bearer Token.
The request header in Postman includes Cache-Control, Content-Type, Content-Length and Host. The request body includes the API KEY, named client_id, client_secret and jwt_token.
In Python, the header includes Cache-Control and Host. Once the request is prepared using the Request.prepare() method, the Content-Lenght and Content-Type are calculated. In the end, the header looks like this:
{'Cache-Control': 'no-cache', 'Host': 'ims-na1.adobelogin.com', 'Content-Length': '774', 'Content-Type': 'application/json'}
The Python request body includes the API KEY, named client_id, client_secret and jwt_token, which I create using PyJWT. In the end, the body looks like this:
{"client_id": "THIS_IS_THE_API_KEY_NAMED_CLIENT_ID", "client_secret": "THIS_IS_THE_CLIENT_SECRET", "jwt_token": "THIS_IS_THE_JSON_WEB_TOKEN_GENERATED_VIA_PYJWT"}
When I try to send my Python request, the API returns a 400 error with a Bad Request message. I leave the Python code available in case the error is something caused by a programming bug.
import os
import dotenv
from requests import Request, Session
dotenv.load_dotenv()
def generate_access_token(token: str):
header = {
"Cache-Control": "no-cache",
"Host": os.getenv('IMS')
}
body = {
"client_id": os.getenv('API_KEY'),
"client_secret": os.getenv('CLIENT_SECRET'),
"jwt_token": token
}
req = Request(method='POST', url="https://"+ os.getenv('IMS') +"/ims/exchange/jwt/", headers=header, json=body)
prep_req = req.prepare()
#print(type(prep_req.headers), ": ", prep_req.headers)
#print(type(prep_req.body), ":", prep_req.body)
ses = Session()
res = ses.send(prep_req)
#print(res.status_code, ":" , res.reason)
It is worth noting that, when printing the data type of the header I get a 'requests.structures.CaseInsensitiveDict', while with the body I get 'bytes'.
I don't know if, when making requests from a programming language, something additional should be added to the header or the body of the request and that's why it's not working.
Thanks in advance.
