3

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"
}
8
  • 1
    Does the API provide any documentation? Authentication should be described there, otherwise few would be able to use the API. Commented Oct 10, 2021 at 21:06
  • Access token are usually used with "Authorization": "Bearer <token>" Commented Oct 10, 2021 at 21:32
  • @ForceBru Yes but all for cURL Commented Oct 10, 2021 at 21:42
  • @RaniSharim Should I strip out the token code part of the JSON that comes back and pass only that in <token> in your example, or all the JSON that comes back? Commented Oct 10, 2021 at 21:44
  • Yes, just the value of "access_token" not the entire json. Also don't post tokens online they should be kept secret. Commented Oct 10, 2021 at 21:47

1 Answer 1

1

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%3A00%3A00

see the str

2021-01-01+00%3A00%3A00

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. Got it working now, but this API seems to not want the date parameters in JSON, instead I had to put them in the query string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.