API calls returning 401 unauthorized in curl, python, etc. Possibly due to LDAP?

  • What Grafana version and what operating system are you using?
    Windows, Grafana 7.5.11

  • What are you trying to achieve?
    I am trying to get data from API calls (any API call right now for testing, but my goal is to try to find all of the teams that a user viewing a dashboard belongs to). I am trying to initially just test out the APIs before incorporating them into my application (Angular/.NET app).

  • How are you trying to achieve it?
    I’ve been trying to call the APIs via curl, Insomnia (like Postman), and via Python script. I created an api key, set it as admin, and used this key in my api calls (shown below).

  • What happened?
    I receive a 401 unauthorized message. Specifically:

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.

  • What did you expect to happen?
    I am expecting a successful response based on what Grafana documentation outlines and based on my research that others have used the same code/commands to get this working.

  • Can you copy/paste the configuration(s) that you are having problems with?

Here are my code excerpts:

curl:

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

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())
  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

Nothing in the logs or on the UI

  • Did you follow any online instructions? If so, what is the URL?

I followed the Grafana documentation for calling APIs, specifically the basic one to get the home dashboard: Dashboard HTTP API | Grafana documentation

I do see that we have basic auth enabled, but we also use LDAP authentication (so when a Grafana user opens the dashboard they do not need to sign in, it takes our organizations credentials). Does that have something to do with why this doesn’t work? I’ve tried adding my AD credentials into the api call when using Insomnia but that didn’t work either.

What do you get with this approach?

import requests

server= "https:<grafanaurl>"
url = server + "/api/dashboards/home"
headers = {"Authorization": "Bearer zimzam"}

print(requests.get(url , headers=headers).json())

I get the following error on the print (removed hostname):
HTTPSConnectionPool(host=‘grafanaurlhere’, port=443): Max retries exceeded with url: /api/dashboards/home (Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)’)))

You got certs issues? you configured grafana with certs but you are calling the api from a resource that is not specifying the cert or is not trusted?

Seems more like a configuration issue with certs and all than a grafana issue?

For testing purposes only maybe try this.

resp = requests.get('https://example.com', verify=True, cert=['/path/to/my/ca.crt'])

any solutions on this, How can we resolve the configuration with certs …

import requests

server= “https://community.grafana.com

url = server + “/api/dashboards/home”

headers = {“Authorization”: “Bearer =##my api key##”}

print(requests.get(url , headers=headers, verify=False).json()) for this code i am getting requests.exceptions.SSLError: HTTPSConnectionPool(host=‘localhost’, port=3000): Max retries exceeded with url: /api/dashboards/home (Caused by SSLError(SSLError(1, ‘[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:992)’))) this error