OAuth token refresh

I'm trying to create an integration with our CRM, however the application doesn't allow generating a set api key I can continue using, it requires users to log in using OAuth2. I'm using the service to process incoming orders sent to a webhook, and automatically enter them into the system. This means there's a highly irregular and unpredictable input. My question is, how do I keep refreshing the token, to make sure it doesn't expire (and require user interaction)?
7 Replies
Jori
Jori14mo ago
The key expires in ~24 hours
zegevlier
zegevlier14mo ago
You can have a CRON job that runs every eg. 12 hours that refreshes the token, so you know it's always valid, or you can use DOs and alarms If you go for the first option, storing the token in KV is probably going to be OK, since even if it takes 1 minute before the new value gets written, it'll still be fine. Unless the old token gets invalidated? In that case you will need to use a consistent storage like DOs
Jori
Jori14mo ago
Alright I'll stick with a cron job then The old token does get invalidated when a new one is generated, I just confirmed that, but that's just fine In case an order comes in during the time it takes to update KV, I just send out an email to manually add the order That's not a problem I'll set the cron to refresh at night to further prevent this Is it possible to test a cron trigger? I wanna run the cron job manually, if possible
zegevlier
zegevlier14mo ago
Not on a deployed worker, but you can test it using the --test-scheduled flag in wrangler.
Jori
Jori14mo ago
Alright Is it possible to combine a cron and a web handler? So that I can have both in the same worker, instead of running 2 workers separately
zegevlier
zegevlier14mo ago
Yup, absolutely! You can set up multiple triggers for the same worker
Jökull Sólberg
Jökull Sólberg12mo ago
I get Miniflare 3 does not support CRON triggers yet Looking through pull requests I can't tell if testing scheduled tasks is supported Oh hold the phone ... hitting __scheduled actually Just Works™