Home > Back-end >  Call API with Python using Token
Call API with Python using Token

Time:10-13

I am trying to call an API using an access token.

Currently I am getting the token like so:

import requests

auth_url = "https://oauth.thegivenurl.com/connect/token"
client_id = "SomeClient_ID"
client_secret = "SomeClient_Secret"
scope = "SomeScope"
grant_type = "client_credentials"

data = {
    "grant_type": grant_type,
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": scope
    }

auth_response = requests.post(auth_url, data=data)

print(auth_response.content)

This produces something like the following:

{"access_token":"eyJhbGdiOihSUzh1NhIsImtpZCI6IjlVUHVwYnBkTXN2RDZ0Ry1ZcDVRUlEiLCJ0eXAiOiJhdCtqd3QifQ.eyJuYmYiOjE2MzM4OTcxNDIsImV4cCI6hTYzMhkwMDc0MiwiaXNzIjoiaHR0cHM6Ly9vYXV0aC56YW1iaW9uLmNvbSIsImF1ZCI6ImFwaTEiLCJjbGllbnRfaWQiOiIwN0I3RkZEOC1GMDJCLTRERDAtODY2OS0zRURBNzUyRTMyNkQiLCJzY29wZSI6WyJhcGkxIl19.GU6lynvQYAAmycEPKbLgHE-Ck189x-a-rVz6QojkBIVpSLu_sSAX2I19-GlTjVWeLKoMVxqEfVq_qIaaQYa5KFmMLHRxP6J-RUgGK8f_APKjX2VNoMyGyAbZ0qXAJCvUTh4CPaRbZ6pexEishzr4-w3JN-hJLiv3-QH2y_JZ_V_KoAyu8ANupIog-Hdg8coI3wyh86OeOSAWJA1AdkK5kcuwC890n60YVOWqmUiAwPRQrTGh2mnflho2O3EZGkHiRPsiJgjowheD9_Wi6AZO0kplHiJHvbuq1PV6lwDddoSdAIKkDscB0AF53sYlgJlugVbtU0gdbXjdyBZvUjWBgw","expires_in":3600,"token_type":"Bearer","scope":"api1"}

Now I would like to call the API and pass the token in a header, but I am unsure how to do this and I have had a couple of goes at online resources

One of my attempts were:

url = "https://anotherurl.com/api/SecuredApi/StaffDetails"

head = {'Authorization': 'token {}'.format(auth_response)}
response = requests.get(url, headers=head)

print(response)

but this gives me a 403 error

Please assist me with pointing out my error

EDIT:

Thanks to @RaniSharim I have made some changes. I now have

import requests
import json

auth_url = "https://oauth.thegivenurl.com/connect/token"
client_id = "SomeClient_ID"
client_secret = "SomeClient_Secret"
scope = "SomeScope"
grant_type = "client_credentials"

data = {
    "grant_type": grant_type,
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": scope
    }

dateparams = {
"StartDateTime": "2021-01-01 00:00:00", 
"EndDateTime" : "2021-10-11 23:59:00"
}

auth_response = requests.post(auth_url, data=data)

# print(auth_response.content)

authjson = auth_response.content
authdata = json.loads(authjson)
token = (authdata['access_token'])

# print(token)

head = {"Authorization": "Bearer "   token}
response = requests.get(url, headers=head, params=dateparams)

print(response.content)

This looks better but I am now getting a 400 error:

"message":"The date range you have specified is in an invalid format. The required format is yyyy-MM-dd HH:mm:ss","status":400}

As best I can see my date ranges are already in the requested format, as set here:

dateparams = {
"StartDateTime": "2021-01-01 00:00:00", 
"EndDateTime" : "2021-10-11 23:59:00"
}

CodePudding user response:

Normally, 400 means front-end error, but when you do GET request

dateparams = {
"StartDateTime": "2021-01-01 00:00:00", 
}
r = requests.get(url, params=dateparams)

print(r.url)

GET url will turn into sth like this:

https://oauth.thegivenurl.com/connect/token?StartDateTime=2021-01-01 00:00:00

see the str

2021-01-01 00:00:00

So if back-end can't handle this right, you'll get this error too

but you can use GET in another way:

requests.get(url, json=dateparams)

This will send your json params perfectly

  • Related