Could Not Authenticate the user Error coming almost every hour

I have been using Fyers API for six months now, apart from small discrepancy in OHLC data, I was not facing any issues. However recently, I am getting the following error :

{‘s’: ‘error’, ‘code’: -22, ‘message’: ‘Could not authenticate the user’}

I am not generating any new auth code or anything. Whenever this happens, I need to generate new auth code and do the entire process again. Can somebody pls help from Fyers? I have been told that I will get a call back regarding this, I am yet to receive any help whatsoever so far.

Hi Dhruv,
How long does it take before you get this error? Because to create new access token the whole process needs to be done again.

Hi Vikram thanks for replying, there is no pattern. I use Fyers API to trade in my mom’s account and mine.

Randomly, while placing orders I get this error. I haven’t changed anything in my code, and previously everything was working fine.

I have found a similar post regarding this,

https://community.fyers.in/post/all-of-the-sudden-fyers-api-is-reporting-error-61b065a9f294b49e482bacbb

If you create a new access token, does the error go away?

Yes, for the time being it does. Then again after a while I get the same error!

I got a call back from Fyers team, I have been told to create new API application altogether and see if the error is solved.

Hi @vikram_sri I am still facing the same error.
Can I get some help from Fyers team?

Dhruv, did you create a new app, and is the authentication error coming only after an hour?
Can you let me know the process in which you create your access token?

The pattern which I am seeing is that whenever I loop through, and place order one by one, one of the account breaks and I get an error the error of could not authenticate user.

However, this doesn’t make sense, if by creating a fyers object results into breaking of the authentication code, it should happen in both the accounts. There is no pattern randomly one of them breaks, the other one runs fine.

   `for x in range(len(login_details)):`

app_id=login_details['app_id'].iloc[x]

#access_token_path= 'Fyers_access_token.txt'

access_token= login_details['access_token'].iloc[x]

fyers = fyersModel.FyersModel(client_id=app_id, token=access_token)

                `   place_order(fyers,stock,order_type,quantity,strike_buy)`

So I read, generate the access token once during the day and store it in login_details dataframe. Then at the time of trade, I loop through this and create fyers object with this application and it’s access token, pass it to the place order function and fire the trade there.

Now during the order placement, one of the accounts result into the error : Could Not Authenticate User. Now, there is no pattern randomly one of the account gets blocked and I need to generate access token again, and then everything works fine and then again after order placement something goes south.

Can you try creating fyers object only once and use that throughout the code?
For example you can do this line inside main function,
fyers = fyersModel.FyersModel(client_id=app_id, token=access_token)

and pass the fyers object to all other functions.

Hey Vikram, just wanted to thank you,

the solution you suggested actually worked. Thanks a lot, cheers mate!

Hey @vikram_sri ,

I found the problem which causes this, its whenever I manually trade into the account, my API connection breaks with the error,

‘Could not authenticate the user’

I am absolutely sure that this is the reason why I get this error, please have a look into it. I can show it live if required, happy to help but please sort this out for the benefit of the users on your platform.

A note to Atlassian, there should be a much simpler way to do this than spending hours searching the Internet looking for an answer. like https://community.fyers.in/post/all-of-the-sudden-fyers-api-is-reporting-error-61b065a9f294b49e482bacbbword search

This website was a lot of fun for me. You are providing a fantastic resource, and you are giving it away for free. fnaf