question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Have invalid grant error

See original GitHub issue
Error: invalid_grant
    at createError (/user_code/node_modules/googleapis/node_modules/axios/lib/core/createError.js:16:15)
    at settle (/user_code/node_modules/googleapis/node_modules/axios/lib/core/settle.js:18:12)
    at IncomingMessage.handleStreamEnd (/user_code/node_modules/googleapis/node_modules/axios/lib/adapters/http.js:201:11)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

I first get the authenticationCode in front end using gapi.auth2.getAuthInstance().grantOfflineAccess() and send the code to server side (server side is firebase cloud functions)

and in server side, I tried like following.

const oauth2Client = new google.auth.OAuth2(
  "XXX",
  "XXX",
  "XXX"
);

oauth2Client.getToken(authenticationCode)  // authenticationCode  is from front end

When I got the error. Any idea how to fix it?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
reidarkcommented, Feb 7, 2020

I’m having this same problem right now. My front-end is running with ReactJS to get code token (GAPI).

window.gapi.load('auth2', function() {
  window.auth2 = window.gapi.auth2.init({
    client_id: OAUTH2_CLIENT_ID,
    scope: OAUTH2_SCOPES
  });
});

window.auth2.grantOfflineAccess().then((code) => console.log(code));
// Returns -> { code: 'XXXXXX...XXXX' }

In my Express server I run the googleapis to get token, but not working.

const code = { code: 'XXXXXX...XXXX' };
const OAuthClient = new OAuth2(
  ID,
  SECRET,
  REDIRECT
);

OAuthClient.getToken(code, (error, tokens) => {
  if (error)  return console.log(error);

  OAuthClient.setCredentials(tokens)
});

Error data:

data: { error: 'invalid_grant', error_description: 'Bad Request' }
0reactions
rhperedacommented, Aug 25, 2020

@reidark, maybe this will help: https://medium.com/@chaimmw86/thanks-for-the-article-really-helpful-36970b83e8a9. I also had this problem in the past, review my configuration following this: https://medium.com/@nickroach_50526/sending-emails-with-node-js-using-smtp-gmail-and-oauth2-316fe9c790a1 and then added the tls: { rejectUnauthorized: false } now is working, hope that help

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to interpret and resolve the "Invalid Grant" error for ...
This happens when the user that connected the Google Data Source in Databox has their permissions changed or revoked to the Google Account....
Read more >
Google OAuth “invalid_grant” nightmare — and how to fix it
Google OAuth “invalid_grant” nightmare — and how to fix it · 1) Server clock/time is out of sync · 2) Not authorized for...
Read more >
"invalid_grant" error when requesting an OAuth Token
Issue symptoms. When I attempt to obtain an access token, I receive the error below. {"error":"invalid_grant",
Read more >
OAuth2 "Invalid Grant" response from server - Stack Overflow
I got annoyed by invalid_grant error instead of the fact that same code is getting me correct access token some times. Shaikh's answer...
Read more >
InvalidGrantError — oauth2-server 4.0.0-dev.2 documentation
The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found