New Tool Radar

AI Agents & Automation

How to Refresh an Expired OAuth Token for an Agent Connector

Refresh an expired OAuth token for an agent connector so scheduled runs keep working.

Schematic with three steps: Check the token expiry before each run; Refresh the token and store the new; Alert the owner if the refresh fails.

An agent connector that talks to Gmail, Sheets, or Slack does so with an OAuth token, and tokens expire. When one does, the scheduled run that depends on it fails with an authentication error, and it keeps failing until someone reconnects the account. The fix is a refresh flow, not a manual reconnect: store the refresh token safely, check the access token’s expiry before each run, and trade the refresh token for a new access token when it is close to expiring. Set up the refresh before the first expiry, because a run that fails on an expired token is a run that already missed its schedule.

What you need

  • A connector that uses OAuth, with its refresh token stored safely
  • A scheduled run that depends on that connector
  • A way to tell the owner when a refresh fails, so a dead token is not silent

Step 1: Check the token expiry before each run

Before the agent calls the connector, check whether the access token is expired or close to it. A check that only happens after a failure is a check that arrives too late.

Step 2: Refresh the token and store the new one

When the token is close to expiring, use the stored refresh token to get a new access token, and store the new one in the same safe place. Never log either token.

Step 3: Alert the owner if the refresh fails

If the refresh fails, stop the run cleanly and tell the owner the connector needs reconnecting. A failed refresh that nobody hears about is a scheduled run that silently stops working.

Worked example: synthetic token refresh check

The table below is a synthetic example. It shows the expected result of token refresh; no token was used.

Check Expected Result
Token checked before the run Yes Pass
Expiring token refreshed Yes Pass
Scheduled run completes Yes Pass
Failed refresh alerts the owner Yes Pass

Expected result (UNTESTED). the expiring token is refreshed before the run, the run completes, and a failed refresh alerts the owner. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • A refresh token can itself expire or be revoked, so a refresh is not a permanent fix.
  • Tokens must be stored safely; a token in a log or a prompt is a leaked credential.
  • Different providers expire tokens on different schedules, so check each connector’s rules.
  • A refresh that happens mid-run can interrupt a call in progress, so refresh before the run starts.

Sources

See all