API unauthorized 401

Hi, I’m trying to connect to grafana from a python script.

import requests
headers = {
    "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXX"
}
url = "http://grafana.staged-by-discourse.com/api/dashboards/home"

r = requests.get(url, headers, verify=False)
print(r)
print(r.json())

And i always get message:
<Response [401]>
{‘message’: ‘Unauthorized’}

But if instead of runing the request in python i do curl, with the same token. I get the expected result.

I’ve been looking to other posts related to the API and the unauthorized message but I didn’t foun any solution

@ramondonadeucaballer if you increase your grafana server’s logging to debug, do you see anything interesting? I would also see if you can print any debugging info using the requests library in Python?

I would also include the exact curl command that you are using. Clearly there is some small detail off here…

Try

r = requests.get(url, headers=headers, verify=False)

headers is named argument

1 Like