How to run twitchio bot forever without manually adding the new token

I understand that if the bot access token expires we must get a new one from the refresh token and replace the token of my bot

bot = commands.Bot(
token=token,
client_id=os.environ[‘CLIENT_ID’],
nick = “mynick”,
prefix=“!”,
initial_channels=[channel]
)

bot.run()

However I don’t understand how I can replace the token automatically when without stopping my server and manually changing the token every time the token expires.

Any help would be extremely appreciated thanks.

Hello, I know how to use the refresh token to get a access token and run the bot however I am running a full stack web application and in my backend so would need to run my bot at all times without stopping the changing the access token myself. Is there a way for the bot to detect when the access token has expired and replace it automatically?

You could just have your backend make the requests to the Refresh Token endpoint, and then store the new access and refresh token, such as in a database, a file, whatever is suitable for your use case.

Then whenever it needs to connect, such as after a disconnect or when the app first starts up, it just gets the token from where you’ve stored them.

Apologies but im very new to twitchio could u give me an example of how you do this? How can I write the code for it to connect and use a new access token whenever there is a disconnection?

I’m not understanding what the issue is. Can you better describe your situation? Am I understanding correctly that you have a web component and a chat connection? You said you understand how to use refresh tokens and your question is about running forever without having to manually add a new token. Where is the issue?

I recommend checking the token’s expiration before reconnecting and refreshing it if it has expired.

Post removed, you leaked your client secret and a refresh token, pleaes revoke and regenerate both

I’m not familiar with twitchio, but a quick glance at the documentation suggests you should consider using Client.event_token_expired instead. It doesn’t look like there is another way to do it without hacking into twitchio’s internals but :person_shrugging:.

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