0

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.

3
  • Usually questions specific to a particular web service are not suitable for Stack Overflow because we cannot accurately recreate your specific authentication environment. You can read about authentication with Grafana's HTTP API here
    – crock
    Commented Jun 16, 2022 at 19:31
  • You can also try the Grafana community forum for support.
    – crock
    Commented Jun 16, 2022 at 19:33
  • 1
    @crock ah fair enough! I will try asking there, thanks. I will leave the question as is here in case anyone may have some input on the API calls themselves or if someone has faced this similar issue as well :) Commented Jun 16, 2022 at 19:52

1 Answer 1

0

try this

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

this worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.