How to refresh an access token in HA

Good day,

I am trying to call an API, that uses Bearer token authentication. However the “access token” is only valid for 2 or 3 hours or something.

They are making use of a much longer living refresh token which you get when you login, you can then go and update that access token.

I am still fairly new to HA and I have not figured out all the config files etc.

But essentially I need to make some sort of script run every hour, to get a new access token, store it in a global variable, and then make my normal resftfull switch use that variable for its authentication header.

Can anyone point me in the right direction with where to start and how to do this?