Passing Token to REST Api using Python

Question

I know that there are a few question on SO that seem to answer this question but none of them get the job done for me.

So here is my code ...

import requests

bearer_token = 'eyJhbGciOiJIUzI1NiIsImtpZCI6IlRFU1QifQ.eyJjbGllbnRpZCI6ImFhZDkyNjc5MmJiMjQ4YjRhZDcyYzk4ZDk0YTZhNTlhIiwidXNlcmlkIjoibmVpbC5oYXJsYW5kQG56cG9zdC5jby5ueiIsInVzZXJuYW1lIjoibmVpbC5oYXJsYW5kQG56cG9zdC5jby5ueiIsImV4cCI6MTQ4MTA2ODM0MSwic2NvcGUiOltdLCJjbGllbnRfaWQiOiJhYWQ5MjY3OTJiYjI0OGI0YWQ3MmM5OGQ5NGE2YTU5YSJ9.BaQk5kCmIWSd2PB2QtT0nbyG5a8FWxSudcbTsS4RvLA'
url = 'https://myapi/apiname/apiversion/endpoint/flow?param1=253&param2=20'

def callMyApi():
  print ("Calling API ...")
  response = requests.get(url,headers={'Authorization': bearer_token})
  print (response.text)

callMyApi()

Here is the respobse I get ...

Calling API ...
{ "error": "missing_access_token", "description": "The access token is missing." }

Now I have tried many varieties such as ...

response = requests.get(url,headers={'Authorization': 'token' + bearer_token})

response = requests.get(url,headers={'Authorization': 'Basic' + bearer_token})

I have even tried base64 encoding just in case there was something interesting happening with that complex bearer_token - even though it is just a string

And nothing works.

Any help would be appreciated


Show source
| rest   | python   | python-requests   2016-12-06 22:12 1 Answers

Answers ( 1 )

  1. 2016-12-06 22:12

    Sorry guys - turns out that the problem lay between the keyboard and the chair :-)

    The code I posted works fine - the actual bearer_token requires the word 'Bearer' in front. Once I added this it worked

◀ Go back