我已经根据Grafana的文档(https://grafana.com/docs/grafana/latest/developers/http_api/dashboard/#gets-the-home-dashboard)编写了API调用,但我总是以401的响应结束。例如,为了获得用于测试路径/api/dashboards/home的基本get响应,我做了以下操作:
在Grafana设置中,我添加了一个api密钥并将其设置为admin。我试过调用api使用curl,失眠(如邮差)和通过Python。(替换api key和grafana url值)
旋度:
curl -k -H "Authorization: Bearer <apikey>" https://<grafanaurl>/api/dashboards/home
反应:
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 [401]>
失眠给出与curl相同的错误信息。
我做错了什么吗?我的组织使用LDAP身份验证来自动登录到Grafana -这可能是它不起作用的原因吗?如果是这样,我该如何处理?我希望能够在应用程序中调用Grafana api。
try this
r = requests.get(url, headers=headers, verify=False)
this worked for me