User Token expire time issue

Hello Everyone!

I´m currently making my own twitch Bot with a !setgame and a !settitle command.
Everything is working fine, i send a request to the api with a user access_token generated with the OAuth authorization code flow method.

Some hours later the commands don’t work anymore because the token expires. Thanks to the refresh_token i can generate a new one and everything is working again.
I have some questions anyway…

• I saw on the forum that my access_token can last about 4 hours. But is there a way to avoid sending an api request every 4 hours ? I need the token to be valid everytime the command is called.
Is there a good way to do that i’m missing ?

• When i send my api request to generate a new access_token, it returns an object with the “expires_in” field.
I thought the number value was the time in second until the token expires. But for example in my last request, the number returned was 14353 which is ~239 hours instead of the 4 hours. Why the value is not matching and is there a way to know exactly what this value means ?

Use Cron/Automation to auto regenerate the token

Or regenerate the token when you needed it.

What API request did you make?

This sounds like you made a call to generate a Client Credentials token and not a call to refresh your token.

To refresh the token, i use the following request :

https://id.twitch.tv/oauth2/token?client_id=placeholder&client_secret=placeholder&refresh_token=placeholder&grant_type=refresh_token

It returns me this object :

Looks correct, your new token is good for four hours.

60 * 60 * 4 = 14400

You have 13374 there (in seconds not minutes)

So checking your math from earier you didn’t do you math right.

14353 seconds is four hours, and my original answer as a pre coffee monday answer

So theres nothing wrong here

Oh… I feel so stupid :sweat_smile:
My brain was too focused on the API documentation that I read for hours to be sure I understood everything.
Guess i have some maths lessons to take…

Thank you for your answer !

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.