Access_token not recognised

Hey I am trying to access the FreeAgent API for the Sandbox account authorised using python script.
"
https://api.sandbox.freeagent.com/v2/company
"
Every thing works fine if i use the Google OAuth2.0 Playground, i.e the response show the following:
Example Data:

{"company":{"type":"UkLimitedCompany","currency":"GBP","mileage_units":"miles","company_start_date":"2010-07-01","sales_tax_registration_status":"Registered"}}

However if i use Python script for accessing the same data, it returns the error at the very last step, at :
"
api_call_headers = {‘Authorization’: 'Bearer ’ + tokens[‘access_token’]}
api_call_response = requests.get(test_api_url, headers=api_call_headers,verify=True)
"
returning response:
"
{“errors”:{“error”:{“message”:“Access token not recognised”}}}"
"
I can only assume the problem might be the with the callback URI, but URI is doing its job just fine by returning the right Authorization code which is exchanged with access_token and refresh_token, everything looks fine but the endpoint is not recognising the access_token. I know Similar topics has been raised by several others, but I haven’t got the solution from any of them, wonder if someone can take a look into this problem. I can send you additional related information if you need.
kind regards, Ammar.

Hi Ammar,

Welcome to the API forum!

If the OAuth 2.0 playground is working as you’d expect, it sounds like you’re making good progress towards working with the API.

You’ve wondered if the callback URI isn’t working - however, if this was the case I wouldn’t expect you to get a token back at all. However, you can check this! I’d check it by using cURL to hit the API, using the access token you’ve picked up. Assuming this gives you a response without error, then you know that the token you have is valid, and the callback URI is working.

That would leave the next most likely source of errors to be the Python script itself. I’m not a pythonista, but it looks like you’re using the Requests HTTP library to query the API. If so, it looks like you can check what HTTP request the library built up to see if it matches what you’d expect:

api_call_headers = {‘Authorization’: 'Bearer ’ + tokens[‘access_token’]}
api_call_response = requests.get(test_api_url, headers=api_call_headers,verify=True)

# This should include the 'Authorization: Bearer TOKEN' information you sent in
print("Request headers: ", api_call_response.request.headers)
# This should include everything else in your request
print("Request body: ", api_call_response.request.body)

Hopefully, those steps will give you a clue as to what is going on!