[google-api] invalid_grant trying to get oAuth token from google
I ran into this same problem despite specifying the "offline"
access_type in my request as per bonkydog's answer. Long story short I found that the solution described here worked for me:
In essence, when you add an OAuth2 Client in your Google API's console Google will give you a "Client ID" and an "Email address" (assuming you select "webapp" as your client type). And despite Google's misleading naming conventions, they expect you to send the "Email address" as the value of the
client_id parameter when you access their OAuth2 API's.
This applies when calling both of these URL's:
Note that the call to the first URL will succeed if you call it with your "Client ID" instead of your "Email address". However using the code returned from that request will not work when attempting to get a bearer token from the second URL. Instead you will get an 'Error 400' and an "invalid_grant" message.
I keep getting an
invalid_grant error on trying to get an oAuth token from Google to connect to their contacts api. All the information is correct and I have tripple checked this so kind of stumped.
Does anyone know what may be causing this issue? I have tried setting up a different client id for it but I get the same result, I have tried connecting many different ways including trying the force authentication, but still the same result.
This is a silly answer, but the problem for me was that I failed to realize I already had been issued an active oAuth token for my google user which I failed to store. The solution in this case is to go to the api console and reset the client secret.
There are numerous other answers on SO to this effect for example Reset Client Secret OAuth2 - Do clients need to re-grant access?
There are two major reasons for invalid_grant error which you have to take care prior to the POST request for Refresh Token and Access Token.
- Request header must contain "content-type: application/x-www-form-urlencoded"
- Your request payload should be url encoded Form Data, don't send as json object.
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.
I found another good article, here you will find many other reasons for this error.
in this site console.developers.google.com
this console board select your project input the oath url. the oauth callback url will redirect when the oauth success
My issue was that I used this URL:
When I should have used this URL:
This was testing a service account which wanted offline access to the Storage engine.
Try change your url for requst to
I encountered the same problem. For me, I fixed this by using Email Address (the string that ends with ...@developer.gserviceaccount.com) instead of Client ID for client_id parameter value. The naming set by Google is confusing here.