为什么我不断收到 Influxdb 未经授权的访问(错误 401)

问题描述 投票:0回答:1

我一直在尝试遵循 influxDB 提供的 python 演练来熟悉它的工作原理,仅仅按照教程进行操作,在相应地执行每个步骤后,我就会收到错误。所有代码都来自他们,但我不断收到相同的错误:

>>> Request: 'POST https://us-east-1-1.aws.cloud2.influxdata.com/api/v2/write?org=PythonTesting&bucket=test1&precision=ns'
>>> Content-Type: text/plain
>>> Accept: application/json
>>> User-Agent: influxdb-client-python/1.37.0
>>> Body: b'census,location=Klamath bees=23i'
<<< Response: 401
<<< Date: Wed, 30 Aug 2023 17:15:22 GMT
<<< Content-Type: application/json; charset=utf-8
<<< Content-Length: 55
<<< Connection: keep-alive
<<< trace-id: a96c933eaca5845f
<<< trace-sampled: false
<<< x-platform-error-code: unauthorized
<<< Strict-Transport-Security: max-age=15724800; includeSubDomains
<<< X-Influxdb-Request-ID: 130ba81a98d7d837641571be3fabc194
<<< X-Influxdb-Build: Cloud
<<< Body: {"code":"unauthorized","message":"unauthorized access"}
Traceback (most recent call last):
  File "C:\Users\user G\PycharmProjects\MEAfile_parsing\InfluxDB_testing.py", line 51, in <module>
    client.write(database=database, record=point)
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client_3\__init__.py", line 106, in write
    raise e
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client_3\__init__.py", line 104, in write
    self._write_api.write(bucket=database, record=record, **kwargs)
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\client\write_api.py", line 378, in write
    results = list(map(write_payload, payloads.items()))
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\client\write_api.py", line 376, in write_payload
    return self._post_write(_async_req, bucket, org, final_string, payload[0])
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\client\write_api.py", line 509, in _post_write
    return self._write_service.post_write(org=org, bucket=bucket, body=body, precision=precision,
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\service\write_service.py", line 60, in post_write
    (data) = self.post_write_with_http_info(org, bucket, body, **kwargs)  # noqa: E501
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\service\write_service.py", line 90, in post_write_with_http_info
    return self.api_client.call_api(
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\_sync\api_client.py", line 343, in call_api
    return self.__call_api(resource_path, method,
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\_sync\api_client.py", line 173, in __call_api
    response_data = self.request(
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\_sync\api_client.py", line 388, in request
    return self.rest_client.POST(url,
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\_sync\rest.py", line 311, in POST
    return self.request("POST", url,
  File "C:\Users\user\PycharmProjects\MEAfile_parsing\venv\lib\site-packages\influxdb_client\_sync\rest.py", line 261, in request
    raise ApiException(http_resp=r)
influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Wed, 30 Aug 2023 17:15:22 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '55', 'Connection': 'keep-alive', 'trace-id': 'a96c933eaca5845f', 'trace-sampled': 'false', 'x-platform-error-code': 'unauthorized', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains', 'X-Influxdb-Request-ID': '130ba81a98d7d837641571be3fabc194', 'X-Influxdb-Build': 'Cloud'})
HTTP response body: {"code":"unauthorized","message":"unauthorized access"}


Process finished with exit code 1

我已尝试多次重置访问令牌环境变量以完全访问所有内容,但没有任何改变。所有变量(例如主机和组织)都是由教程设置的,并根据我的特定设置进行配置。如果您想在此处查看教程,则需要在此处注册:https://cloud2.influxdata.com/signup

是的,我还安装了所有依赖项。这是出于隐私原因对链接和变量进行了一些编辑的代码:

import os, time
from influxdb_client_3 import InfluxDBClient3, Point

token = os.environ.get("INFLUXDB_TOKEN") # this I set in as an environmental variable from an all #access token that is given via the tutorial
org = "PythonTesting"
host = "myhost" # host link is given to me and provided by aws

client = InfluxDBClient3(host=host, token=token, org=org, debug=True)

database="test1"

data = {
  "point1": {
    "location": "Klamath",
    "species": "bees",
    "count": 23,
  },
  "point2": {
    "location": "Portland",
    "species": "ants",
    "count": 30,
  },
  "point3": {
    "location": "Klamath",
    "species": "bees",
    "count": 28,
  },
  "point4": {
    "location": "Portland",
    "species": "ants",
    "count": 32,
  },
  "point5": {
    "location": "Klamath",
    "species": "bees",
    "count": 29,
  },
  "point6": {
    "location": "Portland",
    "species": "ants",
    "count": 40,
  },
}

for key in data:
  point = (
    Point("census")
    .tag("location", data[key]["location"])
    .field(data[key]["species"], data[key]["count"])
  )
  client.write(database=database, record=point)
  time.sleep(1) # separate points by 1 second

print("Complete. Return to the InfluxDB UI.")

您可以再次按照教程进行操作,并看到这是来自 influxdb 的教学介绍。这应该是五分钟的设置,所以我不知道为什么会发生这种情况。我看到还有许多其他堆栈溢出页面也有类似的问题,但没有一个得到完全解答或什至得到承认。我希望这个问题能够得到解决,以便其他人也能解决同样的问题。

python python-3.x token influxdb influxdb-python
1个回答
0
投票

好吧,我弄清楚了我的情况出了什么问题,我仔细检查了所有内容,并意识到,也许令牌没有被正确识别。这样做之后,我打印出了令牌值,它返回了 null。这很奇怪,因为我已在 cmd 中将其设置为系统环境变量。问题是我的 IDE(在我的例子中是 pycharm)默认情况下无法识别环境变量,所以我必须手动添加它们。果然,经过这个小小的改变和数小时的悲伤之后,它起作用了,数据也通过了。

© www.soinside.com 2019 - 2024. All rights reserved.