Unauthorized 401 error for QuertApi of InfluxDBClient object when writing and reading local database value

itshihi
Wed, 16 Oct 2024 23:27:18 +0000
InfluxDB 2

hi, i’m trying to test my spring service code for reading influxdb.
but i got 401 error with the meassage like "2024-10-16 17:52:50 ts=2024-10-16T08:52:50.658230Z lvl=info msg=Unauthorized log_id=0sH8FS4W000 error="token required""

I already checked my token, url, oraganization of config and there are all correct.
It runs with no problem at local or docker environment.

Is there any way for using QueryApi object such like giving token with another method?
I think my spring test failed to deliver the token to InfluxDB Server.

My error code when i tried to activate reading InfluxDB Data with SpringBootTest

HTTP status code: 401; Message: unauthorized access
com.influxdb.exceptions.UnauthorizedException: HTTP status code: 401; Message: unauthorized access
	at com.influxdb.internal.AbstractRestClient.responseToError(AbstractRestClient.java:115)
	at com.influxdb.internal.AbstractQueryApi$1.onResponse(AbstractQueryApi.java:228)
	at com.influxdb.internal.AbstractQueryApi.query(AbstractQueryApi.java:255)
	at com.influxdb.internal.AbstractQueryApi.query(AbstractQueryApi.java:207)
	at com.influxdb.internal.AbstractQueryApi.query(AbstractQueryApi.java:131)
	at com.influxdb.client.internal.QueryApiImpl.query(QueryApiImpl.java:939)
	at com.influxdb.client.internal.QueryApiImpl.query(QueryApiImpl.java:123)
	at com.influxdb.client.internal.QueryApiImpl.query(QueryApiImpl.java:85)

Error code I got from InfluxDB Docker container
2024-10-17 08:25:42 ts=2024-10-16T23:25:42.987073Z lvl=info msg=Unauthorized log_id=0sHvDWBW000 error="token required"

My test code

package org.atg.bems.connection;

import org.atg.bems.entity.ElectricityData;
import org.atg.bems.service.InfluxService;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.time.LocalDateTime;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@ExtendWith(SpringExtension.class)
@SpringBootTest()
public class ReadElectriciyDataListTest {

    @Autowired
    private InfluxService influxService;

    @Test
    @DisplayName("readElectricityDataList 를 통해 db 데이터 직접 읽기 테스트")
    public void readDataList(){

        //given
        LocalDateTime startTime = LocalDateTime.now();
        LocalDateTime endTime = LocalDateTime.now().minusHours(8);
        String measurement = "60주년기념관";

        //when
        List<ElectricityData> electricityDataList = influxService.readElectricityDataList(startTime, endTime, measurement);

        //then
        assertThat(electricityDataList).isNotNull();
        assertThat(electricityDataList.size()).isGreaterThan(0);

        electricityDataList.forEach(System.out::println);


    }


}

the contents of queryapi while debugging test code

3 posts - 3 participants