1

Currently trying to request a json using python but I keep getting a 401 error on response. When I use curl I receive the correct json data.

Curl I use:

curl -H "X-Samanage-Authorization: Bearer API_TOKEN" -H 'Accept: application/vnd.samanage.v2.1+json' -H 'Content-Type: application/json' -X GET https://api.samanage.com/incidents.json

Python that results in 401 error:

import json
import requests

response  = requests.get('https://api.samanage.com/incidents.json', headers={'X-Samanage-Authorization': 'API_TOKEN'})

print(response.status_code)
2
  • Could you please try with headers={'X-Samanage-Authorization': 'Bearer API_TOKEN'} Commented Jan 2, 2018 at 12:44
  • Apart from the above comment: the curl command you used is not the same as what you did in your python code. Does curl -H "X-Samanage-Authorization: Bearer API_TOKEN" -X GET https://api.samanage.com/incidents.json return the correct result? Commented Jan 2, 2018 at 12:47

1 Answer 1

2

Depending on what you're trying to do, your problem is probably that you are not specifying the type of your credentials.

Try doing :

import json
import requests

response  = requests.get('https://api.samanage.com/incidents.json', headers={'X-Samanage-Authorization': 'Bearer ' + API_TOKEN})

print(response.status_code)

Another case, your API_TOKEN is not the actual token if you have not made a mistake while typing your code back. You have a STRING API_TOKEN, but not the actual token.

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

1 Comment

Perfect, was missing the Bearer in the header. I ended up using response = requests.get('api.samanage.com/incidents.json', headers={'X-Samanage-Authorization': 'Bearer API_TOKEN'}) which worked! Thanks for your help

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.