I am encountering an issue where querying data from InfluxDB via FastAPI leads to an “unauthorized access” error during specific times of the day. This typically happens between 12:30 PM and 3:00 PM (IST). Below is the code snippet I am using to fetch the last 60 records from InfluxDB:
async def get_last_60_records(bucket: str, stock_code: str, end_time: str, start_time: str, tail_rows: int):
await InfluxDb.connect_to_influx()
query_api = InfluxDb.client.query_api()
try:
start_time = start_time.replace('.000Z','+05:30')
end_time = end_time.replace('.000Z','+05:30')
last_60_records_query = f'''
import "date"
from(bucket: "{bucket}")
|> range(start: {start_time}, stop: {end_time})
|> filter(fn: (r) => r.stock_code == "{stock_code}")
|> filter(fn: (r) =>
(date.hour(t: r._time) > 3 or (date.hour(t: r._time) == 3 and date.minute(t: r._time) >= 45)) and
(date.hour(t: r._time) < 10 or (date.hour(t: r._time) == 10 and date.minute(t: r._time) < 0)))
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> keep(columns: ["_time", "stock_code", "open", "high", "low", "close", "exchange_code"])
|> tail(n: {tail_rows})
'''
result = await query_api.query_data_frame(last_60_records_query)
return result
except ApiException as e:
error_message = "InfluxDB API error: Check time range or other values used in Influx"
await Logger.error_log(file_name=__name__, func_name="get_last_60_records", error=e)
raise HTTPException(status_code=400, detail=error_message)
this is the error i receive,
Reason: Unauthorized
HTTP response headers: <CIMultiDictProxy(‘Content-Type’: ‘application/json; charset=utf-8’, ‘X-Influxdb-Build’: ‘OSS’, ‘X-Influxdb-Version’: ‘v2.7.10’, ‘X-Platform-Error-Code’: ‘unauthorized’, ‘Date’: ‘Fri, 27 Sep 2024 08:10:00 GMT’, ‘Content-Length’: ‘55’)>
HTTP response body: b’{“code”:“unauthorized”,“message”:“unauthorized access”}’
I have checked everything, but could not able to figure out by my own,
I have tried to call Api with scheduler (every 5 seconds) , It was working fine,
then what is the issue ,
please help me, I have been into this problem from last 1 week,
I have got to resolve this by my own,
HELP ME !!!