Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write fail when using username password based influxdb java client #469

Closed
sw-alpha-romeo opened this issue Nov 22, 2022 · 1 comment
Closed
Labels
question Further information is requested wontfix This will not be worked on
Milestone

Comments

@sw-alpha-romeo
Copy link

Steps to reproduce:
execute below code by changing host, user name and password

`

   InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder().url("").authenticate("admin", "helloworld".toCharArray()).bucket("test").org("my-org").build();

   InfluxDBClient client = InfluxDBClientFactory.create(influxDBClientOptions);  
   
    WriteApi writeApi = client.getWriteApi()) 

    Point point = Point.measurement("mem")
                .addTag("host", "host3")
                .addField("used_percent3", 29.43234543);

    writeApi.writePoint(point);
    client.close();

`

Expected behavior:
expected that successful write will have happen

Actual behavior:
Getting unauthorized access error
com.influxdb.exceptions.UnauthorizedException: unauthorized access
Note:-
1> Using same credentials which I used in the code, I am able to login to InfluxDB web UI console.
2> If I create a token from web UI console after login using credentials used in the code and use
this new token then in InfluxDBClientOptions, then write operation gets successful
Specifications:

  • Client Version:
  • InfluxDB Version: 2.5
  • JDK Version: 11
  • Platform: mac
@bednar
Copy link
Contributor

bednar commented Nov 23, 2022

Hi @sw-alpha-romeo,

thanks for using our client.

The WriteApi uses background thread to batch writes into InfluxDB, so you have to close writeApi before closing the client. The following code will works:

package example;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.InfluxDBClientOptions;
import com.influxdb.client.WriteApi;
import com.influxdb.client.write.Point;

public class InfluxDB2Example {

    public static void main(final String[] args) {

        InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder()
                .url("http://localhost:9999")
                .authenticate("my-user", "my-password".toCharArray())
                .bucket("my-bucket")
                .org("my-org")
                .build();

        try (InfluxDBClient client = InfluxDBClientFactory.create(influxDBClientOptions)) {

            try (WriteApi writeApi = client.makeWriteApi()) {

                Point point = Point.measurement("mem")
                        .addTag("host", "host3")
                        .addField("used_percent3", 29.43234543);

                writeApi.writePoint(point);
            }
        }
    }
}

or you can you WriteApiBlocking:

package example;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.InfluxDBClientOptions;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.write.Point;

public class InfluxDB2Example {

    public static void main(final String[] args) {

        InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.builder()
                .url("http://localhost:9999")
                .authenticate("my-user", "my-password".toCharArray())
                .bucket("my-bucket")
                .org("my-org")
                .build();

        try (InfluxDBClient client = InfluxDBClientFactory.create(influxDBClientOptions)) {

            WriteApiBlocking writeApi = client.getWriteApiBlocking();

            Point point = Point.measurement("mem")
                    .addTag("host", "host3")
                    .addField("used_percent3", 29.43234543);

            writeApi.writePoint(point);
        }
    }
}

btw the recommended authentication is by token.

Regards

@bednar bednar closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2022
@bednar bednar added question Further information is requested wontfix This will not be worked on labels Nov 23, 2022
@bednar bednar added this to the 6.8.0 milestone Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants