0

I am trying just simple connect to influxdb which started on localhost.

When I try just client:

influx --host 192.168.0.2 --port 8086

It works.

But when I try python:

import os
import json
from influxdb_client import InfluxDBClient, Point, WritePrecision

influxdb_url = os.environ['INFLUXDB_URL']
influxdb_host = os.environ['INFLUXDB_HOST']
influxdb_port = os.environ['INFLUXDB_PORT']

client = InfluxDBClient(url=influxdb_url)

I got exception

TypeError: __init__() missing 1 required positional argument: 'token'

But cli client can not connect without any tokens? Why do I need it in python? What token to use?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Cherry
  • 31,309
  • 66
  • 224
  • 364
  • 2
    Is url a valid parameter? https://influxdb-python.readthedocs.io/en/latest/api-documentation.html – DaveStSomeWhere Nov 02 '21 at 07:23
  • Does this answer your question? [Use Influxdb client in python - Error : 'InfluxDBClient' object has no attribute 'create\_database'/' get\_list\_database '](https://stackoverflow.com/questions/64206590/use-influxdb-client-in-python-error-influxdbclient-object-has-no-attribute) – Yaroslav Kornachevskyi Nov 02 '21 at 07:28
  • @DaveStSomeWhere According to that page, `token` isnt either. Try looking at https://github.com/influxdata/influxdb-client-python – OneCricketeer Nov 02 '21 at 16:32

3 Answers3

1

InfluxDB 2.0 requires an authentication token for all API access. The client library is using the same APIs as anything else, so it needs a token to securely connect to InfluxDB.

You can create an auth token from the CLI or the GUI itself.

mhall119
  • 522
  • 2
  • 3
1

You could use username and password instead of token.

import influxdb_client
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS

_influxdb_client = InfluxDBClient(
            url=f"http://localhost:8086",
            username=os.environ['INFLUXDB_USERNAME]',
            password='os.environ['INFLUXDB_PASSWORD']',
            verify_ssl=False)

I'm using influxDB v2.0.9 and client influxdb_client library v1.36.1

If you are using influxdb v1.8.x with influxdb_client library then you should pass username and password in the token field for authorization.

import influxdb_client
from influxdb_client import InfluxDBClient
from influxdb_client.client.write_api import SYNCHRONOUS

_influxdb_client = InfluxDBClient(
            url=f"http://localhost:8086",
            token=f"os.environ['INFLUXDB_USERNAME]:os.environ['INFLUXDB_PASSWORD']",
            verify_ssl=False)
0

According to https://influxdb-client.readthedocs.io/en/stable/api.html you can also auth. with username+pw on InfluxDB 2.x (works for me on 2.2.0).

e.g.: client = InfluxDBClient(url=url,username='',password='',ssl=True, verify_ssl=True, org=org)

gCW
  • 1
  • 1