Requesting token frequency question

Hello there! I’m fairly new to using APIs and oauth.

I wrote a script that gets a token using grant_type=client_credentials and then checks on two specific server statuses, texting me if the status changes to UP or DOWN.

It does this every 30 sec, and I’m wondering if it’s worth putting the token in a file and using that until expires_in reaches close-to zero, then refresh it, or if the method I’m using is fine.

Sorry if this is a super-noob question lol

It’s not a huge deal either way, but the fewer requests you make, the less chance you have that one of them will fail or time-out or lag. So yeah I’d cache it somewhere for a while.

Just be aware that a token might expire before its timeout, so if you get an unauthorized error, refresh your token before trying again.

1 Like

Ah, yeah that makes sense. Also, I didn’t know it could expire before the timeout. I’ll look into the different responses so I can parse for it correctly.

Thank you!

1 Like

Yeah technically they’re supposed to be valid for the entire timeout, but I’ve been bitten before by a rare case of that not happening. I would cache the token for the entire duration, then after Blizz reset/rebooted something on their end, the token was no longer valid, and my app was broken until it refetched a new token on its usual timer.