So i have an case interview programming problem which is about using API with Python to send information from a source URL to a target URL. Information from the source URL purely JSON data, and it is ready to send to the target URL. But in order to send the JSON data to the target URL, i require access-token and a API-version headers:
Here is the headers that the post request needs so i can post the JSON data. This is from the documentation which the company provided.
I have extracted the access-token from the target API by using a get request and passing in credenstials:
Base_URL = "i can't show the URL, but imagine there is a URL here"
client_ID = "abc"
client_SECRET = "123"
api_VERSION = "v3"
grant_TYPE = "client_credentials"
headers = {
'Client-Id': client_ID,
'Client-Secret': client_SECRET,
'Grant-Type':grant_TYPE,
'Api-Version':api_VERSION
}
request = requests.get(Base_URL, headers=headers)
token_info = request.json()
token = token_info.get('access_token')
print(token)
I am able to retrieve the token perfectly fine. But the problems is when i have to post the data to the target API using the token.
Target api requires the access_token, but when i write the following code which provides the access token and JSON data to the URL:
post_URL = "Nope can't show the URL"
headers = {
'Authorization':f'bearer {token}',
'Api-Version':api_VERSION,
'Content-Type': "application/json"
}
for user in user_array:
request = requests.post(post_URL, headers=headers, json=json.dumps(user))
print(request.text)
print(request.text) gives me the follow error message: {“message”:”Invalid access token”}.
Can anyone help me find out what i am doing wrong? Is it maybe the interviewer who has sendt me the case that have made a mistake?
Ahadd Rashid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.