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.

Refresh token and invalid_grant error confusion

See original GitHub issue

I have successfully implement the whole OAuth process using googleapis in my app and it works but I still get the invalid_grant error every now and then. I searched for a solution to this and it seems there is a lot of confusion as to how the tokens should work.

When I want to make a call to one of the APIs I set the credentials like so:

oauth2Client.setCredentials({
    access_token: "ACCESSTOKEN",
    refresh_token: "REFRESHTOKEN"
})

and set an event listener for .on('tokens') to get the new set of tokens in case the current ones are expired and when that happens I save them to the db but the invalid_grant error is still thrown on that call. Only the next call with updated tokens eventually works (or sometimes doesn’t and the whole integration needs to be reauthenticated).

Should I send the refresh_token with every API call in case the tokens are expired and new ones will be issued? Some people suggested this issues a new token on every call and eventually exceeds the token quota for the user.

How do I get a new working token before making the API call so that there’s no invalid_grant error?

There seems to be a widespread confusion about how those tokens work. It would be nice if it was explained in the documentation.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
bl4ckckcommented, Jan 27, 2021

@bcoe thanks, I think I’ve resolved my problem here.

When I put access_token and refresh_token to setCredentials, it works and automatically replaces the previous access token.

oAuth2Client.setCredentials(tokens); //access_token, refresh_token, expiry

And I can obtain new access token after refreshing with this function

oAuth2Client.getAccessToken((err, token) => {
        console.log("new access_token: "+token)
})
0reactions
alexander-fenstercommented, Jul 21, 2022

@parkerproject Is this still a problem? If it is, please open a new issue describing the environment you’re in, the specific API you’re calling, and providing as much details as possible. Also, we updated the documentation for refresh tokens here.

Since this issue is originally about a different problem, I’ll close it now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RE: General Google Oauth2 confusion ('invalid_grant' when ...
Kindly note that the invalid_grant usually indicates an issue with the refresh token. That said, you'll need to regenerate credentials and ...
Read more >
Error Code: null, Error Message: Failed to refresh token ... - Help
The cause of this error is because your Authorization Code, is either expired or has been used before. That is why you get...
Read more >
Google OAuth “invalid_grant” nightmare — and how to fix it
The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not ...
Read more >
Using Refresh Token Exception { "error" : "invalid_grant" }
The reason of the "Invalid grant" error may be due to the refresh token not working. This could be ...
Read more >
"invalid_grant", "error_description": "expired access/refresh ...
I'm a bit confuse of the error because at line of my code I'm still request for access_token with the code from salesforce...
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