Usually, Google OAuth2.0 mechanism is working great.
But sometimes (thus far only two times for more than 6 months) I've experienced strange behaviour:
Requests to Google APIs return Invalid Credentials (401) error. Refreshing the access token (using the stored refresh token) does not help.
Here is some structured output I've got when testing this issue:
+ ------------------------------------------------------------------------- + | 1.TRYING TO REFRESH THE TOKEN. | | 2.DONE REFRESHING THE TOKEN. | + ------------------------------------------------------------------------- + | access: **************************************************** | | refresh: ********************************************* | | expires: 3600 | | created: 2013-07-23 13:12:36 | + ------------------------------------------------------------------------- +
I've also tried to verify the "fresh" access token by sending requests to https://www.googleapis.com/oauth2/v1/tokeninfo
+ ------------------------------------------------------------------------- + | 1. TRYING TO CHECK THE TOKEN . | | 2. DONE CHECKING THE TOKEN THE TOKEN. | + ------------------------------------------------------------------------- + | issued_to: ************.apps.googleusercontent.com | | audience: ************.apps.googleusercontent.com | | user_id: ************ | | expires_in: 3600 | | email: **********@gmail.com | | verified_email: 1 | | access_type: offline | | scopes:: | + ------------------------------------------------------------------------- + | https://www.googleapis.com/auth/userinfo.email | | https://www.googleapis.com/auth/userinfo.profile | | https://www.googleapis.com/auth/plus.me | | https://www.googleapis.com/auth/drive | + ------------------------------------------------------------------------- +
But when I try to access drive feed the response is:
Error calling GET https://www.googleapis.com/drive/v2/files (401) Invalid Credentials domain: global reason: authError message: Invalid Credentials locationType: header location: Authorization
We also experienced the same issue with calendars. So:
Normally when the token is revoked "invalid_grant" error is returned when trying to refresh the token.
Questions
Per the Google API docs on errors & error codes:
https://developers.google.com/drive/handle-errors#401_invalid_credentials
401: Invalid Credentials
Invalid authorization header. The access token you're using is either expired or invalid.
error: {
errors: [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
This matches your version of the error exactly, and so is very probably what Google thinks is wrong with your request.
But, as you well know, Google API requests can return errors that are distinctly unhelpful to actually diagnosing the problem. I have gotten "Invalid Credentials" errors for a number of reasons. It is almost always really because I have made some sort of change that I thought would not matter, but really does.
My first thought (shot in the dark here) would be to go to the Google API console:
https://code.google.com/apis/console
Googles auth token verifier ( https://www.googleapis.com/oauth2/v1/tokeninfo ) can return a valid response, but maybe the client secret or client id will have been changed.
Even tiny changes in the response body can also cause this error.
I don't know how you are making requests, whether by REST calls or a client lib, but I use the ruby lib which allows a command line interface to making API calls. I have found this & the OAuth2 Playground very helpful in diagnosing Google API calls.
Just an FYI: I have only gotten 2 errors from the Google API: "Invalid Credentials" and "Insufficient Permissions". The latter has almost always had to do with bad scopes. The former is just about everything else.
I would also say that if you have only experienced 2 errors in 6 months, you are lucky!
I had this problem when I tried experimenting with changing the redirect url in google console and then updating my json credentials file on server. I had to clear the session variables before starting afresh. So in your project just do this once:
session_start(); //starts a session
session_unset(); //flushes out all the contents previously set
Remember to remove the session_unset()
after dry running it once.
I'm on Development environment. I had this problem too.
First I tried refreshing the credentials. No result. Then I deleted my app (since I'm still on development enviroment, that was ok, but BE CAREFUL WITH THIS ACTION if you're already using this on production), created a new one, updated the credentials JSON on the client... still, no result.
I solved it by opening on a new browser instance which wasn't logged in my Google Account (Private Browsing, since I'm on Firefox), logged on my Google Account once again, and tried using my client (which is a Web Application). I was redirected to the authorization screen as expected and after that, it worked fine for me.
Maybe this behavior is due to a limitation which Google describes as follows:
There is currently a limit of 50 refresh tokens per user account per client. If the limit is reached, creating a new token automatically invalidates the oldest token without warning. This limit does not apply to service accounts.
There is also a larger limit on the total number of tokens a user account or service account can have across all clients. Most normal users won't exceed this limit but a developer's test account might.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With