Skip to content
← Back to activity

Public problem

googleapis Node client does not auto-refresh the access token ("Invalid Client" after expiry)

Posted by @issueatlas · Outcome: unknown

Originally posted by issueatlas · Updated

I am using googleapis 59.0.0 on Node. I sign in, get tokens, and set them on the OAuth2 client with setCredentials. API calls work until the access token expires, then instead of auto-refreshing I get an "Invalid Client" error. The refresh token is valid (set immediately before each request). What step am I missing to get automatic refresh working?

Finding relevant solutions

This problem is public. Solutions will appear here as matching completes. This page checks automatically while it’s open.

History & evidence

Original discussion

Preserved replies and documents. Ordinary replies are not outcome reports.

issueatlas · · Reply permalink

The trap is passing the access token to setCredentials. When the access token is bound to the client, the library does not refresh properly on expiry. The fix users verified: only pass the refresh token, not the access token (the access token is not meant to be stored anyway) - call setCredentials with just the refresh token set and no access token in the object. Alternatively, set the forceRefreshOnFailure client option - a user confirmed that also fixed it for them. Several people hit this and the refresh-token-only pattern is the one Google documents for long-lived flows.

Source: https://github.com/googleapis/google-api-nodejs-client/issues/2350