Invalid Grant - Need Help OAuth2 - Urgent Pelase!

30,682 views
Skip to first unread message

naman jindal

unread,
Oct 28, 2013, 10:54:33 AM10/28/13
to adwor...@googlegroups.com
Hi Guys,

I am trying to set up my first Adwords API application. To begin with, I am only trying to set up the examples which are provided with the client libraries.

So, I just downloaded the example and all dependent files. I provided all the information in the properties file:
Refresh Token, Client Id, Client Secret, Client Customer Id, Developer Token, User Agent

(Though I am not sure about the User Agent part.)

Now when I simly try to run the program to get list of campaigns in the account, I get an error on the first step (OAuth2):

Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}


Please suggest what am I missing out on. It is very urgent!

Thanks for your time and help.


Regards,
Naman Jindal

Josh Radcliff (AdWords API Team)

unread,
Nov 1, 2013, 5:05:04 PM11/1/13
to adwor...@googlegroups.com
Hi Naman,

The "invalid_grant" error usually means you tried to use the same authorization code to get more than one developer token.  The authorization code is the string returned after you click Accept from the URL provided by the offline credentials example, and you can only use an authorization code once.

To resolve this, please do the following:
  1. Log out of all Google accounts in your browser
  2. Log into your AdWords account at https://adwords.google.com
  3. Run the offline credentials example (again)
  4. Navigate to the URL provided by the offline example and click Accept
  5. Copy the refresh token printed by the offline example into your AdWords configuration file
Regarding the User Agent, that is an optional string that helps identify requests from your application.

Best,
Josh, AdWords API Team

naman jindal

unread,
Nov 4, 2013, 3:48:47 AM11/4/13
to adwor...@googlegroups.com
Thanks for your help Josh.

I have more difficulties and questions relating to setting up the Application. 
But I think I would better give in some more efforts before putting up more questions.

Regards,
Naman Jindal

Anash P. Oommen (AdWords API Team)

unread,
Nov 5, 2013, 6:45:33 AM11/5/13
to adwor...@googlegroups.com
Hi Naman,

What language and client library are you using?

Cheers,
Anash

naman jindal

unread,
Nov 5, 2013, 8:54:45 AM11/5/13
to adwor...@googlegroups.com
Hi Anash,

I am using Java client Library.

I am trying to build a Jsp Servlet application that would allow me to perform certain operations in a user friendly way. I am not sure how this is done but I am trying hard and taking help from forums/documentation to get this done. Currently I am stuck at the fist step of OAuth2 authorization.

Here's a brief of what I have done till now :

1. I downloaded the example provided by the API team with the documentation, and transformed it into a JSP Servlet program.
2. Now I am trying to play around with ads.properties file to fill in all the details necessary for connection to the API.
 2a) For client Id I went to the link : https://code.google.com/apis/console#access -> API Access -> Create Client ID 
 2b) Now, since I am in the initial phase of development, I am just trying to run the application from eclipse on local host, so I tried to specify the redirect url as -: http://localhost

3. From there I got my client Id and client secret. Using this I generated a refresh token, and then I added the developer token,clientcustomerId that I had to the properties file and tried to run GetCampaigns file where in I got the error - > Invalid Grant

Can you please suggest if I am following the right process or not? If you need I can share the confidential details in a private message to you.

Thanks for your time!

Regards,
Naman Jindal

naman jindal

unread,
Nov 6, 2013, 8:48:51 AM11/6/13
to adwor...@googlegroups.com
Hey Anash,

Please provide updates on this!

Thanks

naman jindal

unread,
Nov 7, 2013, 9:40:19 AM11/7/13
to adwor...@googlegroups.com
Hey Guys,

I am still stuck on this invalid grant exception. Please help me out.
I have tried the steps suggested by Josh but its not helping. Please advice.

Exception in thread "main" com.google.api.ads.common.lib.exception.OAuthException: Credential could not be refreshed.
at com.google.api.ads.common.lib.auth.OfflineCredentials.generateCredential(OfflineCredentials.java:149)
at adwords.axis.v201309.basicoperations.GetCampaigns.main(GetCampaigns.java:51)
Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:248)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.ads.common.lib.auth.OAuth2Helper.callRefreshToken(OAuth2Helper.java:70)
at com.google.api.ads.common.lib.auth.OfflineCredentials.generateCredential(OfflineCredentials.java:144)
... 1 more



naman jindal

unread,
Nov 8, 2013, 9:04:15 AM11/8/13
to adwor...@googlegroups.com
Hey Guys,

Can anyone who has knowledge about this exception help me out on how can I resolve this.

This is really very urgent and I would be very much thankful for any ind of help/suggestions.

Thanks and Regards,
Naman Jindal


Anash P. Oommen (AdWords API Team)

unread,
Nov 8, 2013, 9:19:56 AM11/8/13
to adwor...@googlegroups.com
Hi Naman,

The problem is that invalid grant is a generic error that says your OAuth signup failed. It doesn't give any more details, and that is by design. That makes it all the more difficult to debug the issue.

Let's start one step at a time. I'm sure you might have tried these steps before, but it would be nice if you could do this again from beginning and help me figure out at what stage the code fails:

1. Were you able to able to run the offline credentials application and generate a refresh token and access token?
2. Did you copy this refresh token to your ads.properties file and run a Java code example?
3. Could you double-check if you don't have any trailing spaces on the refresh token in the ads.properties?
4. When you run code example, does the refresh token belong to the customer pointed to by clientCustomerId, or its parent / ancestor MCC?

Cheers,
Anash P. Oommen,
AdWords API Advisor.

naman jindal

unread,
Nov 8, 2013, 11:01:01 AM11/8/13
to adwor...@googlegroups.com
Hey Anash,

Thanks for taking this up.


1. Were you able to able to run the offline credentials application and generate a refresh token and access token? 
      Yes, I ran the GetRefreshToken.java file to generate a link from where I got the refresh token.

2. Did you copy this refresh token to your ads.properties file and run a Java code example?
      Yes.
 
3. Could you double-check if you don't have any trailing spaces on the refresh token in the ads.properties?
      I have checked it, no trailing spaces. 

4. When you run code example, does the refresh token belong to the customer pointed to by clientCustomerId, or its parent / ancestor MCC?
       I am not sure about this. I am going to send you a private message in next 5 mins keeping in all the details that I have in my properties files. I hope you can catch the mistake there in.

slege...@gmail.com

unread,
Nov 8, 2013, 4:05:20 PM11/8/13
to adwor...@googlegroups.com
I'm having the same problem.

I did the following for a test account :
* download the PHP API library
* use developer token from production account
* get "native" client ID + client secret
* add client ID + client secret to API library
* run the "GetRefreshToken" file to get a refresh token
* add refresh token to API library

I managed to create a simple PHP app using these settings.

Then, my developer key was approved.

I got the "native" client ID + client secret for the production account as well and generated a refresh token. I followed the same procedure I followed with my test account.

Yet, I still keep getting the following error : An error has occurred: { "error" : "invalid_grant" }

I repeated the process several times. I have no idea what could be wrong...




Op vrijdag 8 november 2013 15:19:56 UTC+1 schreef Anash P. Oommen (AdWords API Team):

naman jindal

unread,
Nov 10, 2013, 9:35:31 AM11/10/13
to adwor...@googlegroups.com

Hi Anash,

Did you get my message?
I am waiting for your inputs/suggestions.

Thanks!
Message has been deleted

Danial Klimkin

unread,
Nov 12, 2013, 7:15:18 AM11/12/13
to adwor...@googlegroups.com
Hello Naman,


I suggest clearing the config and trying to do the OAuth flow again.

Before starting please make sure to log off from any google account in your browser and clear cookies. When following the auth URL you will be presented with the login screen. Make sure to log in with you AdWords API account details.


-Danial, AdWords API Team.

John Slegers

unread,
Nov 12, 2013, 11:07:44 AM11/12/13
to adwor...@googlegroups.com
I tried that... over and over and over...

The last time, I made sure to clear the browser's entire history. That way, I was sure I wasn't logged in into any system that uses Google credentials. Yes, I still see no improvement.

I don't think it's anything we're doing wrong. I seems like a bug or a problem with our accounts on Google's end.





Op dinsdag 12 november 2013 13:15:18 UTC+1 schreef Danial Klimkin:

John Slegers

unread,
Nov 12, 2013, 11:09:24 AM11/12/13
to adwor...@googlegroups.com
I tried that.... over and over and over. The last time, I even made sure to clear the browser's entire history.

I don't think it's anything we're doing wrong. I seems like a bug or a problem with our accounts on Google's end.





Op dinsdag 12 november 2013 13:15:18 UTC+1 schreef Danial Klimkin:
Hello Naman,

naman jindal

unread,
Nov 13, 2013, 2:42:32 AM11/13/13
to adwor...@googlegroups.com
Hi Anash,

The issue is now resolved. I was making a lame mistake. 

I was considering the code received from the url as the refresh token and never realized that I had to use that code to get the refresh token.

All solved now.

Thanks!
Reply all
Reply to author
Forward
0 new messages