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.
Solved! Go to Solution.
Views
Replies
Total Likes
@edgar_herrera Could you try below and let me know if it has worked?
import os
import dotenv
from requests import Request, Session
dotenv.load_dotenv()
def generate_access_token(token: str):
header = {
"Cache-Control": "no-cache",
"Content-Type": "application/json" # manually set the content type
}
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()
ses = Session()
res = ses.send(prep_req)
# Print the response status and content for debugging
print(res.status_code, ":" , res.reason)
print(res.text) #print the response
Views
Replies
Total Likes
Hi @edgar_herrera the moment you update the request json=body to data=body magic happens
req = Request(method='POST', url="https://"+ os.getenv('IMS') +"/ims/exchange/jwt/", headers=header, data=body)
Also to highlight The Service Account (JWT) credentials is being deprecated (Your applications using the Service Account (JWT) credentials will stop working after Jan 1, 2025) in favor of the new OAuth Server-to-Server credentials(OAuth 2.0)
@edgar_herrera Could you try below and let me know if it has worked?
import os
import dotenv
from requests import Request, Session
dotenv.load_dotenv()
def generate_access_token(token: str):
header = {
"Cache-Control": "no-cache",
"Content-Type": "application/json" # manually set the content type
}
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()
ses = Session()
res = ses.send(prep_req)
# Print the response status and content for debugging
print(res.status_code, ":" , res.reason)
print(res.text) #print the response
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies