Invalid API key?

  • What Grafana version and what operating system are you using?
    Grafana open source in a container, itself in an Azure web app

  • What are you trying to achieve?
    Using a Jupyter Notebook & Python, I would like to programmatically retrieve the list of dashboards in my Grafana instance, and for each of them, retrieve the JSON file that describes their layouts.

  • How are you trying to achieve it?

  • I created a Service Account in my Grafana, granted myself an Admin role, and generated a token.

  • Using Jupyter Notebook, I ran the following code:

headers = {"Authorization": f"Bearer {os.getenv(myToken)}"}

s = requests.Session()
s.headers = headers
r = s.get(url + "/api/search?query=&", headers=headers)
dashboards = r.json()
print(dashboards)
  • What happened?
    I’m getting this response:
'{"extra":null,"message":"Invalid API key","messageId":"api-key.invalid","statusCode":401,"traceID":""}\n'
  • What did you expect to happen?
    I expected my token to be a valid API key (I believe these terms share the same meaning?).

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

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    I am not sure how to access the logs.

  • Did you follow any online instructions? If so, what is the URL?
    I tried to follow:

  • Grafana’s documentation, although it does not provide Python code snippets;

  • this post

Can you please help? Thank you so much!

Welcome @vincentteplitxky

Check out the docu

you are missing something in the query param

GET /api/search?folderIds=0&query=&starred=false HTTP/1.1
Accept: application/json
Content-Type: application/json
Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk
1 Like

Thanks, I changed my headers line to:

headers = {
    "Authorization": "Bearer "+ myToken,
    "Accept": "application/json",
    "Content-Type": "application/json",
}

other lines of code remain unchanged.
And now the request works. Thanks!

1 Like