Token is expiring on random days


We run a cron job that runs every 30 minutes because we need to fetch data from infusionsoft api. The token are stored in a json file along with the expire time of the access token. The script checks if token has expired ie compares unix timestamp of the token validity and current timestamp.

If expired makes request for new token. And stores new access token and refresh token for future use along with a future expiring timestamp. This works for 3/4 days and max 1 week and suddenly the token are not renewed anymore.

Does anyone know why this is happening?

I am not entirely sure what you are asking but, it seems like you might have a synchronization issue. A refresh token can only be used once, so if on thread refreshes the token and another thread is making a request at the same time then the refresh will fail for the last one in. This is a guess based on the limited information. It would be helpful to actually get the error you are seeing on refresh along with any headers you are receiving.

Thanks for the reply.

This is the error that I’m receiving:
Request string grant_type=refresh_tokenDATA got {“error”:“invalid_grant”,“error_description”:“Invalid refresh token”}
Obtained new access and refresh token from Token server.
Error occurred obtaining new token.

There is no threading. It’s a php script where we first check if access token is valid or not. If not then we use the refresh token to get new refresh token and new access token and store it again. This process works just fine for few days or a week and then suddenly we can not refresh the token as it says the error above.

99% of the time that means that the refresh token that was issued was already used to get a new access token. Refresh tokens are single use, is it possible you are not storing the new refresh token when you obtain the new access token? Using an old refresh token will cause that error or using an expired refresh token (refresh tokens are good for 6 months). We have also seen where people think their cron is only running one at a time, but in reality it was running more than once causing some concurrency issues. Just some guesses to maybe check out.

Thanks Brad,

Some token request using refresh token are returning empty responses, our assumption is that responses will always return either error or json with access_token and refresh_token … so the problem was solved by checking if the response is empty then we do not update anything because the refresh token is still valid.

The issue we are having and we were able to reproduce is that the token returns empty every once in awhile. Do you know what is going on? Or can point us into the right direction

That is indeed strange. Does the empty response correspond to a certain response code as well or just the normal 200?