Unable to connect to local influx DB

HI There,

I have installed the influxDB on my local and i am using the below python code to write the data into influxDB. I was able to access the UI on http://localhost:8086. and i was able to ping it. However, when i run the code, i am getting unauthorized error. Any help would be appreciated.

123123:~ peter$ influx ping
OK
from influxdb import InfluxDBClient
from datetime import datetime


    client = InfluxDBClient('localhost', 8086, 'username', 'password', 'database_name') 
    client.create_database('database_name')
    client.get_list_database()
    client.switch_database('database_name')

    # Inserting data into InfluxDB each row at a time
    print("InfluxDB injection process started.")
    for row in df.iterrows():
        influxJson = [
                    {
                        "measurement":"test7",
                        "time" : datetime.utcnow().isoformat() + "Z",
                        "tags": {
                            'ResiliencyTier':'targetResiliencyTier',
                            'lob' : 'teacher'
                        },
                        "fields": {
                            columns[0][0] : str(row[1][0]),
                            columns[1][0] : str(row[1][1])
                        }
                    }
                ]
        client.write_points(influxJson) 
    print("process completed.")

startProcess()

Error:

Traceback (most recent call last):
  File "/Users/peter/Library/Python/3.8/lib/python/site-packages/influxdb/client.py", line 378, in request
    raise InfluxDBClientError(err_msg, response.status_code)
influxdb.exceptions.InfluxDBClientError: 401: {"code":"unauthorized","message":"Unauthorized"}

Thank You

Welcome to the community.
Which InfluxDB version do you have installed? I guess you have installed InfluxDBv2 but you are using the client library for version 1.x.x ?

InfluxDBv2 python client:

GitHub - influxdata/influxdb-client-python: InfluxDB 2.0 python client

InfluxDBv1 python client:

GitHub - influxdata/influxdb-python: Python client for InfluxDB