Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'invalid_grant' error with google oauth from second time

I am using google oAuth for my python application in which I have feature to automatically logging in by google into my app. When I try to login first time, it will be successfull, but from next time if I will login it doesn't success and each time I gets 500 internal server error.

When I checked error logs, I got following error message on 'credentials = oAuthFlow.step2_exchange(code)' line

Failed to retrieve access token: {
    "error" : "invalid_grant"
}

I have valid clientId registered on google. Can anybody tell me why is it happening. I am using python 2.7.

like image 752
Workonphp Avatar asked Aug 30 '13 10:08

Workonphp


People also ask

What is invalid_grant in OAuth?

RFC 6749 OAuth 2.0 defined invalid_grant as: The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

Why am I getting the error message “invalid grant”?

THE "INVALID GRANT" ERROR MAY BE RETURNED FOR THE FOLLOWING REASONS 1 The user has revoked your access. 2 The refresh token has not been used for six months 3 The user changed passwords and the refresh token contains Gmail scopes 4 The user has exceeded the maximum number of granted (live) refresh tokens

Why is the provided authorization grant or refresh token invalid?

The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

Why are my refresh tokens being rejected by Google?

invalid_grant The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client. Right — so for literally any reason possible, our tokens are getting rejected by Google.


1 Answers

It could be caused by any number of things, including ...

  1. User has withdrawn permission
  2. The scopes have changed
  3. Google has retired your refresh token
  4. Bugs in your code which are presenting the wrong refresh token. Remember you will ONLY get a refresh token the first time through. On subsequent calls the refresh token will be null because you should have saved it in your database.

I get it a lot when I use the same user for both testing and live use since the two refresh tokens tend to overwrite each other.

The good news is that whatever the cause, the solution is always the same. You need to force a re-authorization from your user.

like image 176
pinoyyid Avatar answered Oct 07 '22 00:10

pinoyyid