python-3.x InfluxDB -未授权访问问题

t98cgbkg  于 2023-05-23  发布在  Python
关注(0)|答案(1)|浏览(374)

我是InfluxDB的新手,在尝试使用Python influxdb库连接到我的InfluxDB示例时遇到了“未授权访问”问题。我收到以下错误消息:原因:未经授权HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': 'v2.7.0', 'X-Platform-Error-Code': 'unauthorized', 'Date': 'Wed, 17 May 2023 15:03:50 GMT', 'Content-Length': '55'}) HTTP response body: {"code":"unauthorized","message":"unauthorized access"}
我从InfluxDB文档中尝试使用python客户端库连接的代码片段,正在尝试熟悉该过程:

import influxdb_client, os, time
 from influxdb_client import InfluxDBClient, Point, WritePrecision
 from influxdb_client.client.write_api import SYNCHRONOUS

 token = os.environ.get("INFLUXDB_TOKEN")
 org = "Org_Name"
 url = "http://localhost:8086"

 write_client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)

 bucket="SensorDB"

 write_api = write_client.write_api(write_options=SYNCHRONOUS)
   
 for value in range(5):
   point = (
     Point("measurement1")
     .tag("tagname1", "tagvalue1")
     .field("field1", value)
   )
   write_api.write(bucket=bucket, org="Org_Name", record=point)
   time.sleep(1) # separate points by 1 second

这个错误的原因是什么,我该如何解决它?
任何指导或建议将不胜感激。谢谢!your text

but5z9lq

but5z9lq1#

假设您的InfluxDB示例正确设置了令牌,问题可能与如何设置INFLUXDB_TOKEN环境变量(或其值)有关。要进行故障排除,您可以尝试暂时打印出token的值并确保它是正确的令牌。

相关问题