Search code examples
pythonauthenticationcurlpython-requestsaccess-token

Python request with access token not working


When I am running below API query using curl I am getting json response

curl -X GET --header 'Accept: application/json' --header 'token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c' 'https://localhost:10010/v1/machines'

But when I am trying to run same query using python requests I am getting access denied error must supply authorization token with result code 403. Below is the python code which I am trying

import requests

url = 'https://localhost:10010/v1/machines'
head = {'Authorization': 'access_token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'}

response = requests.get(url, headers=head)

print response.text

Please let me know if I am doing anything wrong here. Thanks.


Solution

  • The reason for the failure is because you are passing the incorrect header.

    With cURL, you pass

    --header 'token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'
    

    With requests, the same format is to be followed, and you must have:

    head = {'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c'}
    response = requests.get(url, headers=head)