Unauthorized access with official python writing example

I am trying to write data with the python code you can see in the GUI (IP:8086). My influxdb is version 2.5. I have the right influxdb-client for this version. I have tried creating new token, but the result is the same. I have followed all the steps in the tutorial.

The code is:

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 = “censured”
url = “https://us-west-2-1.aws.cloud2.influxdata.com/

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


write_api = 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=“censured”, record=point)*
  • time.sleep(1) # separate points by 1 second*

And the output is this one:

influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({‘Date’: ‘Mon, 28 Nov 2022 12:30:02 GMT’, ‘Content-Type’: ‘application/json; charset=utf-8’, ‘Content-Length’: ‘55’, ‘Connection’: ‘keep-alive’, ‘trace-id’: ‘b834ca17b533f0e8’, ‘trace-sampled’: ‘false’, ‘x-platform-error-code’: ‘unauthorized’, ‘Strict-Transport-Security’: ‘max-age=15724800; includeSubDomains’, ‘X-Influxdb-Request-ID’: ‘2ae0c8dfaf88a4d59bc51e69332d3cdc’, ‘X-Influxdb-Build’: ‘Cloud’})
HTTP response body: {“code”:“unauthorized”,“message”:“unauthorized access”}

1 Like

try: url = “http://localhost:8086

No, same result.

influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({‘Content-Type’: ‘application/json; charset=utf-8’, ‘X-Influxdb-Build’: ‘OSS’, ‘X-Influxdb-Version’: ‘v2.5.0’, ‘X-Platform-Error-Code’: ‘unauthorized’, ‘Date’: ‘Wed, 30 Nov 2022 15:40:27 GMT’, ‘Content-Length’: ‘55’})
HTTP response body: {“code”:“unauthorized”,“message”:“unauthorized access”}

Hi Did you manage to resolve this? I am running InfluxDB using docker. Following the official python example tutorial I get the same influxdb_client.rest.ApiException: (401). Any help would be much appreciated.

Hi @DevElec,

thanks for using our client.

Do you have configured INFLUXDB_TOKEN environment property?

Instead token = os.environ.get(“INFLUXDB_TOKEN”) you can use something like token = "my-token"