Python influxdb bucket connection

sorry Iam so frustated sitting so long on this propaly stupid issue.

I have a Sensor, which data I would like to upload to my bucket created on influxdb

CODE

#!/usr/bin/python
# -*- coding:utf-8 -*-
from datetime import date

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

bucket = "SkyCamLA"
token = os.environ.get("INFLUXDB_TOKEN")
org = "paulmatteschk@googlemail.com"
url = "https://europe-west1-1.gcp.cloud2.influxdata.com"

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

while True:
  
    SolarIrradiance = (ADC_Value[0] * REF / 0x7fffffff)
               print("ADC1 IN%d = %lf" %(0, (ADC_Value[0] * REF / 0x7fffffff)))   # 32bit
                
                write_api = client.write_api(write_options=SYNCHRONOUS)
                p = influxdb_client.Point("my_measurement").tag("location", "Laubegast").field("DNH", 
                SolarIrradiance)
                        
                write_api.write(bucket=bucket, org=org, record=p)
                time.sleep(40)

ERROR

  File "/home/pi/.local/lib/python3.9/site-packages/influxdb_client/_sync/rest.py", line 243, in request
    raise ApiException(http_resp=r)
influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Date': 'Mon, 25 Jul 2022 10:57:15 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '55', 'Connection': 'keep-alive', 'trace-id': 'bad35e71b8e0d1f5', 'trace-sampled': 'false', 'x-platform-error-code': 'unauthorized', 'Strict-Transport-Security': 'max-age=15724800; includeSubDomains', 'X-Influxdb-Request-ID': '5583707876ff46d48ed7476f999c9ba1', 'X-Influxdb-Build': 'Cloud'})
HTTP response body: {"code":"unauthorized","message":"unauthorized access"}

in /etc/influxdb/influxdb.config is http true…

I dont get it

Hi @Bra1nsen,

You are using INFLUXDB_TOKEN system environment variable to set authorisation token. Do you have correctly set this system variable?

Regards

good point, i did as described:

“export INFLUXDB_TOKEN=pMa7…”

but its not showing in printenv

ok it works after entering the token manually in the python script.

Do you know how to set up the environment var correct in linux?

You have to add the export of the INFLUXDB_TOKEN to your ~/.bashrc.

1 Like

youre my hero, thanks alot bednar