get – Python requests library how to pass Authorization header with single token

get – Python requests library how to pass Authorization header with single token

In python:

(<MY_TOKEN>)

is equivalent to

<MY_TOKEN>

And requests interprets

(TOK, <MY_TOKEN>)

As you wanting requests to use Basic Authentication and craft an authorization header like so:

VE9LOjxNWV9UT0tFTj4K

Which is the base64 representation of TOK:<MY_TOKEN>

To pass your own header you pass in a dictionary like so:

r = requests.get(<MY_URI>, headers={Authorization: TOK:<MY_TOKEN>})

I was looking for something similar and came across this. It looks like in the first option you mentioned

r = requests.get(<MY_URI>, auth=(<MY_TOKEN>))

auth takes two parameters: username and password, so the actual statement should be

r=requests.get(<MY_URI>, auth=(<YOUR_USERNAME>, <YOUR_PASSWORD>))

In my case, there was no password, so I left the second parameter in auth field empty as shown below:

r=requests.get(<MY_URI, auth=(MY_USERNAME, ))

Hope this helps somebody 🙂

get – Python requests library how to pass Authorization header with single token

This worked for me:

access_token = #yourAccessTokenHere#

result = requests.post(url,
      headers={Content-Type:application/json,
               Authorization: Bearer {}.format(access_token)})

Leave a Reply

Your email address will not be published. Required fields are marked *