kiwrezc2000
kiwrezc2000

Reputation: 11

Calling Grafana API returns 401 unauthorized

I have written API calls according to Grafana's documentation (https://grafana.com/docs/grafana/latest/developers/http_api/dashboard/#gets-the-home-dashboard) but I always end up with a response of 401. For example, just to get a basic GET response for testing with the path /api/dashboards/home, I have done the following:

On the Grafana settings, I have added an api key and set it to admin. I have tried calling the api using curl, Insomnia (like Postman) and via Python. (Replaced api key and grafana url values)

curl:

curl -k -H "Authorization: Bearer <apikey>" https://<grafanaurl>/api/dashboards/home

response:

curl: (6) Could not resolve host: GET
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

python:

import json
import requests

server= "https:<grafanaurl>"
url = server + "/api/dashboards/home"
headers = {
    "Authorization": "Bearer <apikey>",
    "Accept": "application/json",
    "Content-Type": "application/json"
    }
r = requests.request("GET", url, headers=headers, verify=False)
print(r)
# print(r.json())

response:

<Response [401]>

Insomnia gives the same error message as curl.

Am I doing something wrong? My organization uses LDAP authentication to automatically log us in to Grafana - could this be a reason why this doesn't work? If so, how would I work with this? I want to be able to call Grafana apis from within an application.

Upvotes: 0

Views: 3413

Answers (1)

Sumanth Kalluri
Sumanth Kalluri

Reputation: 3

try this

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

this worked for me

Upvotes: 0

Related Questions