You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

获取数据源详情正常,编辑时Grafana API返回404错误

Grafana API更新数据源返回404问题排查

问题场景

已在Grafana中创建Infinity类型数据源,尝试通过API更新其Bearer Token以刷新身份验证,但调用localhost:3000/api/datasources/uid/:uid接口时返回404 Not Found错误。可正常通过API创建数据源,但无法更新现有数据源,操作流程参考Grafana官方数据源API文档。

环境信息

使用Grafana开源版Docker容器,预装Infinity插件,启动命令:

docker run -d -p 3000:3000 --name=grafana -e "GF_INSTALL_PLUGINS=yesoreyeram-infinity-datasource" grafana/grafana-oss

现有代码

grafana_api_token = '<my api token>'
new_access_token = '<my new bearer token>'
my_data_source = 'my_data_source'

grafana_header = {"authorization": f"Bearer {grafana_api_token}", "content-type":"application/json;charset=UTF-8"}
grafana_datasource_url = f"http://localhost:3000/api/datasources/name/{my_data_source}"

firebolt_datasource_resp = get(url=grafana_datasource_url, headers=grafana_header)
full_datasource = loads(firebolt_datasource_resp.content.decode("utf-8"))
datasource_uid = full_datasource["uid"]

update_token_url = f"http://localhost:3000/api/datasources/uid/{datasource_uid}"

new_data = {"id": full_datasource["id"],
            "uid": full_datasource["uid"],
            "orgId": full_datasource["orgId"],
            "name": "new_data_source",
            "type": full_datasource["type"],
            "access": full_datasource["access"],
            "url": full_datasource["url"],
            "user": full_datasource["user"],
            "database": full_datasource["database"],
            "basicAuth": full_datasource["basicAuth"],
            "basicAuthUser": full_datasource["basicAuthUser"],
            "withCredentials": full_datasource["withCredentials"],
            "isDefault": full_datasource["isDefault"],
            "jsonData": full_datasource["jsonData"],
            "secureJsonData": {            
                    "bearerToken": new_access_token
                }
}

update_bearer_token_resp = post(url=update_token_url, data=dumps(new_data), headers=grafana_header)

核心问题与解决办法

  1. HTTP请求方法错误:Grafana更新数据源的API要求使用PUT请求,而非代码中的POST。这是导致404的最直接原因,将post()替换为put()即可修复。
  2. UID有效性验证:确认从/api/datasources/name/:name接口获取的UID与Grafana界面中数据源详情页显示的UID一致,避免因UID错误导致路径无效。
  3. API令牌权限:确保使用的Grafana API令牌拥有Editor或Admin角色权限,无权限的令牌会返回403或404(部分场景下)。
  4. 请求体完整性:保留原有数据源的所有必填字段,避免因缺失字段导致接口异常,仅修改secureJsonData.bearerToken即可。

内容的提问来源于stack exchange,提问作者Matthew Darwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 06:55:18