FG
๐Ÿ”Œ APIs & SDKsGoogle

Refresh token automatically on HTTP 401 or 403 response?

Freshover 9 years ago
Mar 14, 20260 views
Confidence Score85%
85%

Problem

I'm using the API (v1.0.10) in the following way: [code block] When my token is expired I get an HTTP 401 back from the server. However, I'd expect the library to automatically refresh my token and make the request again. It doesn't. I figured out that a workaround is to add the `expiry_date` when calling setCredentials, e.g. [code block] But this isn't mentioned in the docs. I'd expect `expiry_date` to be optional and the API to auto refresh the token.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Moderate Confidence Fix
84% confidence100% success rate2 verificationsLast verified Mar 14, 2026

Solution: Refresh token automatically on HTTP 401 or 403 response?

Low Risk

I ran into a similar issue. What works for me is to set expiry_date to true when setting the credentials and I don't have a date availble. This will force a token refresh, you can then store the client's expiry_date after using it. [code block]

84

Trust Score

2 verifications

100% success
  1. 1

    I ran into a similar issue. What works for me is to set expiry_date to true when

    I ran into a similar issue. What works for me is to set expiry_date to true when setting the credentials and I don't have a date availble. This will force a token refresh, you can then store the client's expiry_date after using it.

    text
    oauth2Client.setCredentials({
          access_token: user.google.accessToken,
          refresh_token: user.google.refreshToken,
          expiry_date: true
        });

Validation

Resolved in googleapis/google-api-nodejs-client GitHub issue #261. Community reactions: 1 upvotes.

Verification Summary

Worked: 2
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

google-apioauthsdk:rotating_light:triage-me