googleapis / googleapis/google-api-nodejs-client

Periodic GaxiosError: invalid_grant on gmail API

Open
#3,011 0 comments 0 reactions 0 assignees View on GitHub
priority: p3 type: question
Dominant language
TypeScript
Stars
12.2k
Forks
2k
Avg merge
1d 9h
Merged PRs (30d)
24

Description

Hiya, I have a node server running some automatic email parsing. About once a week the server will crash returning and invalid_grant error from the following code:

authorize(cred, callback){
const {client_secret, client_id, redirect_uris} = cred.installed;
const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]);
fs.readFile(process.env.TOKEN_PATH, (err, token) => {
var now = new Date().getTime();
if (err) return this.getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}

I am then required to delete my access/refresh token and obtain a new one from the API before I can begin processing emails. I'm not particularly versed in reading HTTP errors (yet) so I can't work out if the error is complaining about the refresh token or providing a new one. The full error is below:

GaxiosError: invalid_grant
0|ttb | at Gaxios._request (/root/ttb/ttb/node_modules/gaxios/build/src/gaxios.js:130:23)
0|ttb | at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
0|ttb | at async OAuth2Client.refreshTokenNoCache (/root/ttb/ttb/node_modules/google-auth-library/build/src/auth/oauth2client.js:174:21)
0|ttb | at async OAuth2Client.getRequestMetadataAsync (/root/ttb/ttb/node_modules/google-auth-library/build/src/auth/oauth2client.js:284:17)
0|ttb | at async OAuth2Client.requestAsync (/root/ttb/ttb/node_modules/google-auth-library/build/src/auth/oauth2client.js:357:23)
0|ttb | at async email_parser.parseMessages (/root/ttb/ttb/app/email.js:179:15) {
0|ttb | response: {
0|ttb | config: {
0|ttb | method: 'POST',
0|ttb | url: 'https://oauth2.googleapis.com/token',
0|ttb | data: 'refresh_token=REDACTED&grant_type=refresh_token',
0|ttb | headers: [Object],
0|ttb | paramsSerializer: [Function: paramsSerializer],
0|ttb | body: 'refresh_token=REDACTED&grant_type=refresh_token',
0|ttb | validateStatus: [Function: validateStatus],
0|ttb | responseType: 'json'
0|ttb | },
0|ttb | data: {
0|ttb | error: 'invalid_grant',
0|ttb | error_description: 'Token has been expired or revoked.'
0|ttb | },
0|ttb | headers: {
0|ttb | 'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
0|ttb | 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
0|ttb | connection: 'close',
0|ttb | 'content-encoding': 'gzip',
0|ttb | 'content-type': 'application/json; charset=utf-8',
0|ttb | date: 'Thu, 23 Jun 2022 19:22:45 GMT',
0|ttb | expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
0|ttb | pragma: 'no-cache',
0|ttb | server: 'scaffolding on HTTPServer2',
0|ttb | 'transfer-encoding': 'chunked',
0|ttb | vary: 'Origin, X-Origin, Referer',
0|ttb | 'x-content-type-options': 'nosniff',
0|ttb | 'x-frame-options': 'SAMEORIGIN',
0|ttb | 'x-xss-protection': '0'
0|ttb | },
0|ttb | status: 400,
0|ttb | statusText: 'Bad Request',
0|ttb | request: { responseURL: 'https://oauth2.googleapis.com/token' }
0|ttb | },
0|ttb | config: {
0|ttb | method: 'POST',
0|ttb | url: 'https://oauth2.googleapis.com/token',
0|ttb | data: 'refresh_token=REDACTED&grant_type=refresh_token',
0|ttb | headers: {
0|ttb | 'Content-Type': 'application/x-www-form-urlencoded',
0|ttb | 'User-Agent': 'google-api-nodejs-client/8.0.2',
0|ttb | 'x-goog-api-client': 'gl-node/18.3.0 auth/8.0.2',
0|ttb | Accept: 'application/json'
0|ttb | },
0|ttb | paramsSerializer: [Function: paramsSerializer],
0|ttb | body: 'refresh_token=REDACTED&grant_type=refresh_token',
0|ttb | validateStatus: [Function: validateStatus],
0|ttb | responseType: 'json'
0|ttb | },
0|ttb | code: '400'
0|ttb | }

This is likely a user error on my part (improperly handling the refresh token or something I'm not sure. Any guidance available?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.