Learn Build Community Opportunities Hosted n8n Blog About Careers Contact Log in

Home / Blog / Fix

Fix

N8n oauth2 refresh token: production-ready refresh pattern

A production-tested recipe to stop n8n workflows failing when OAuth2 access tokens expire, using a refresh manager workflow, HTTP Request and a secure token store.

Workflows fail when access tokens expire because there is no reliable refresh-login pattern in place. For a stable production setup implement an automated refresh flow that uses a scheduled "refresh manager" workflow, a secure token store, and the HTTP Request and Code nodes to perform and retry refreshes. This solves the n8n oauth2 refresh token problem reliably.

Why this happens

Access tokens are short lived. When an API returns 401, the token is expired. If your workflow only stores the access token and not a fresh, durable refresh token, or if refreshes are manual, the next run will fail. Many people rely on one-off OAuth logins in the editor, which work during development but fail in production when tokens rotate or are revoked.

Production-tested recipe: n8n oauth2 refresh token

  1. Create a secure place to store tokens. Use a private Airtable base, a PostgreSQL table, or a secure Google Sheet with restricted access. Store these fields: access_token, refresh_token, expires_at (UNIX timestamp), client_id and client_secret if you must, otherwise keep client credentials in n8n Credentials.
  2. Build a scheduled Refresh Manager workflow that runs every 5 minutes. Steps inside it:
  • Read the stored tokens (Airtable, Postgres, or Sheets). If expires_at is within a buffer (for example now + 60 seconds), call the token endpoint to refresh.
  • Use the HTTP Request node to POST the refresh request. Typical body type is application/x-www-form-urlencoded with parameters grant_type=refresh_token, refresh_token, client_id and client_secret as required by the API.
  • On success, update the token store with the new access_token, optional new refresh_token and compute a new expires_at. On failure, send an alert and stop further scheduled jobs until the issue is resolved.
  1. Make your API workflows read the token store before making requests. Use HTTP Request to set header Authorization: Bearer <access_token>.
  2. Add a 401 retry handler: if an API call returns 401, call the Refresh Manager (use the Execute Workflow node) then retry the API call once. If it still fails, raise an alert with error body and last known expires_at.

n8n-specific implementation notes

Use these n8n nodes and features for a clean, maintainable setup:

  • HTTP Request node for both the token refresh POST and API calls. Set the request body to Form-Data or Raw - application/x-www-form-urlencoded depending on the provider.
  • Code node to compute expires_at from expires_in and to parse responses. Prefer a single place to compute timestamps and place a 60 second safety buffer.
  • Execute Workflow node to call your Refresh Manager from other workflows. This keeps refresh logic centralized.
  • Store client secrets either in n8n Credentials or an encrypted secrets store. Create a credential in Credentials > New Credential > OAuth2 API for quick built-in flows when the credential supports automatic refresh. If you use custom refresh logic, do not store secrets in plain nodes outputs.

Example minimal refresh body in the HTTP Request node (set Content-Type to application/x-www-form-urlencoded):

grant_type=refresh_token&refresh_token=REFRESH_TOKEN&client_id=CLIENT_ID&client_secret=CLIENT_SECRET

Google, YouTube and n8n Cloud managed OAuth gotchas

Google and YouTube behave differently in two ways you must watch for. First, Google only returns a refresh_token on the first consent unless you request access_type=offline and force consent with prompt=consent. Second, if you use n8n Cloud managed OAuth credentials many users share the same Google project. That can trigger per-project quotas or YouTube upload quota errors. See the community thread about YouTube upload quota on n8n Cloud for real examples and discussion here.

If you rely on n8n Cloud managed OAuth and encounter quota or missing refresh token issues, switch to per-user OAuth credentials or a service account where supported. For Google APIs, ensure the initial consent included access_type=offline.

How to stop it happening again: n8n oauth2 best practices

  • Centralize refresh logic in one Refresh Manager workflow so all API flows use the same tokens and you can patch refresh rules in one place.
  • Keep a 60 to 120 second safety buffer before expiry to avoid races.
  • Use alerts. If refresh fails with a response like invalid_grant or invalid_client, send Slack, email, or SMS and pause integrations until manual intervention.
  • Prefer per-user OAuth credentials for high-volume APIs to avoid per-project quotas and make revocation less disruptive.
  • Test the full flow: revoke a refresh token in the provider console, then confirm your Refresh Manager detects failures and notifies you.

When to get help

If refresh requests return invalid_grant or you see repeated 401 after a refresh, the refresh token was revoked or rotated by the provider. Check the provider console and user consents. If you suspect n8n behavior, post the minimal logs and node configuration on the n8n community. For patterns and advice from other builders, see the forum discussion about OAuth2 best practices here.

For a Google Sheets specific OAuth problem and how we fixed it in a workflow, see our guide Fix google sheets oauth error in n8n.

If you build automations for Nigerian SMEs or freelancing clients, use this pattern and you will avoid the most common production failure: expired tokens that stop workflows cold. Keep token handling centralized, use the HTTP Request and Code nodes, and add retries and alerts.

Learn to build automations like this with Automate Naija.

Frequently asked questions

What is the simplest way to refresh tokens automatically in n8n?

The simplest reliable approach is a scheduled Refresh Manager workflow that reads a secure token store, calls the API token endpoint with the refresh_token using the HTTP Request node, updates the store with the new access token and expires_at, and alerts on failures.

Can I rely on n8n Credentials built‑in OAuth?

n8n Credentials can handle OAuth flows for many providers and will automatically refresh for credential‑backed requests. However, n8n Cloud managed OAuth can hit provider quotas or not return refresh tokens for Google. For production reliability use a central refresh manager or per-user credentials when necessary.

How do I handle 401 failures mid workflow?

Add a 401 handler: when an API call returns 401, call your Refresh Manager (via Execute Workflow ), update tokens, then retry the API call once. If it still fails, raise an alert and stop further retries to avoid loops.

Why didn’t Google return a refresh token during login?

Google returns a refresh token only on first consent unless you request access_type=offline and use prompt=consent . If you used managed OAuth or consent was already given, you may not get a refresh token and must reauthorize with those parameters or use per-user credentials.


Build it with us. The community is free and the build track teaches this kind of automation week by week: see pricing or join the community.

Keep reading

A new fix lands here every morning, picked from what builders are stuck on right now.