Offline Refresh Token Can't Generate

1,655 views
Skip to first unread message

Pravin Yadav

unread,
Apr 6, 2016, 3:34:26 AM4/6/16
to AdWords API Forum
Hi,

I am using Google AdWords Version v201603 in that taking adwords account data Offline using Dot Net Web Service that time i trying to generate tokens offline using(delegate) and store that in Database and then accessing this tokens for data retrieve that time access token generating each time correctly but refresh token not generating.
 
follow is code i am using: 

         oAuth.OnOAuthTokensObtained += delegate(AdsOAuthProvider pp)
               
           {

                        OAuth2ProviderForApplications oAuth =(provider as OAuth2ProviderForApplications);

                       string refresht = oAuth.RefreshToken;
                       string accesst = oAuth.AccessToken;
                       Date updateo= oAuth.UpdatedOn;
                       int expire = oAuth.ExpiresIn;

            }


Its throwing following error:

             Failed to refresh access token.
              {
                  "error" : "invalid_grant"
              }

Please suggest me any another way or any correction/mistaken in existing code to generate offline Refresh Token. 


Thanks & Regards,

  Pravin Yadav

Zweitze

unread,
Apr 6, 2016, 5:53:09 AM4/6/16
to AdWords API Forum
You want to generate a refreshtoken once, store that in the database, then use that refreshtoken with software you wrote?

Consider using OAuth2 for applications.
The .NET client library has (or used to have) a sample application called OAuthTokenGenerator. I created some refreshtokens with that application, store that in a database and have software using those tokens every day.

OAuthTokenGenerator may not be present in the current .NET library (no idea actually), I use a copy from a library three years old. Since the authentication is not bound to AdWords API versions, it is still working.

I hope this helps.

Josh Radcliff (AdWords API Team)

unread,
Apr 6, 2016, 1:00:55 PM4/6/16
to AdWords API Forum
Hi Pravin,

The OAuth 2.0 spec provides the following description for that error:

         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.

I would recommend verifying that the redirect URI specified on your project at https://console.developers.google.com matches what you're using in your requests, and also verify that the refresh token has not been revoked. You might also want to check out the README on our github repo for more details and code examples.

Thanks,
Josh, AdWords API Team

Pravin Yadav

unread,
Apr 12, 2016, 6:49:27 AM4/12/16
to AdWords API Forum
Hi,

        Yes i generated it manually using OAuth2 Console's OAuth2ClientId & OAuth2ClientSecret and then it stored it in database and used it in my application but it temporary it's not useful in web service it need to               generate automatically after once run Web Service code because my service execute back-end(background Process run anytime) and it's offline.   

        in my service i want generate refresh token each time when my service run so token need to generate automatically (offline) and replace each time it in table's column of my database.

        Some days ago this work properly worked with above(Delegate) code but now it not generated refresh token (generated only access token) i need refresh token. 

   So please reply the same.


Thanks & regards,

   Pravin Yadav

     

Josh Radcliff (AdWords API Team)

unread,
Apr 12, 2016, 9:39:26 AM4/12/16
to AdWords API Forum
Hi Pravin,

You mentioned "I want to generate refresh token each time", but you only need to generate a refresh token once for each user whose credentials you want to use. When generating the refresh token, you should set the access_type to offline so that you can retrieve new access tokens without reprompting the user.

If you generate a new refresh token every time, you are likely to run into the limit on the # of active refresh tokens -- see section 4 - "Refresh the access token, if necessary" of the OAuth guide.

Thanks,
Josh, AdWords API Team

Pravin Yadav

unread,
Apr 13, 2016, 12:44:57 AM4/13/16
to AdWords API Forum


Hi Josh,

              Ok, But in my application if i generate refresh token manually using "Google AdWords API Project OAuthTokenGenerator" and then stored it in my database and later access it in web service for 3600 (Sec)                   time it working properly not showing any error but after 3600(Sec) it showing error.

              Failed to refresh access token.
              {
                  "error" : "invalid_grant"
              }

              Then what will be the problem even access token generated each time new and i have refresh token then here no need to show error. But refresh token is expired after 3600(Sec) and it not generated                             automatically, but when create refresh token manually then working properly for 3600(Sec).  

       Then what should i need to do give me any solution. 

Josh Radcliff (AdWords API Team)

unread,
Apr 13, 2016, 11:44:57 AM4/13/16
to AdWords API Forum
Hi Pravin,

Since your refresh token is becoming invalid after 3600 seconds = 1 hour, that suggests that you are not setting access_type to offline in your request to generate the refresh token. Please double check the step that generates the initial request and make sure that you pass access_type=offline there.

Thanks,
Josh, AdWords API Team

Pravin Yadav

unread,
Apr 14, 2016, 4:00:09 AM4/14/16
to AdWords API Forum

  Hi Josh,

               Yes,You are correct i did'nt set access_type="offline" in a proper manner, its working now.

               Thanks for your suggestion's,it was very helpful for me.

Bret Williams

unread,
Sep 9, 2016, 4:17:12 AM9/9/16
to AdWords API Forum
Hi Josh - I upgraded our AdWords library from v201601 to v201603 and we're now getting this error.   Did the authentication change in this release?  

System.ArgumentNullException: Value cannot be null.

Parameter name: Looks like your application is not configured to use OAuth2 properly. Required OAuth2 parameter RefreshToken is missing. You may run Common\\Utils\\OAuth2TokenGenerator.cs to generate a default OAuth2 configuration.

   at Google.Api.Ads.Common.Lib.OAuth2ProviderBase.ValidateOAuth2Parameter(String propertyName, String propertyValue)

   at Google.Api.Ads.Common.Lib.OAuth2ProviderForApplications.RefreshAccessToken()

   at AdWords.Jobs.Google.GoogleReportsJob.GetAPIUser(AdProvider credential, String clientCustomerId)

   at AdWords.Jobs.Google.GoogleReportsJob.ProcessReport(AdReportJob& adReportJob)

 

   at Google.Api.Ads.Common.Lib.OAuth2ProviderBase.ValidateOAuth2Parameter(String propertyName, String propertyValue)

   at Google.Api.Ads.Common.Lib.OAuth2ProviderForApplications.RefreshAccessToken()

   at AdWords.Jobs.Google.GoogleReportsJob.GetAPIUser(AdProvider credential, String clientCustomerId)

   at AdWords.Jobs.Google.GoogleReportsJob.ProcessReport(AdReportJob& adReportJob)

Anash P. Oommen (AdWords API Team)

unread,
Sep 12, 2016, 10:13:50 AM9/12/16
to AdWords API Forum
Hi Bret,

Could you confirm if your App.config / Web.config have the following keys in it?


     
<add key='AuthorizationMethod' value='OAuth2' />
     
<add key='OAuth2ClientId' value='xxxxxx.apps.googleusercontent.com' />
     
<add key='OAuth2ClientSecret' value='xxxxxx' />
     
<add key='OAuth2RefreshToken' value='xxxxxx' />

You could also provide this at runtime by setting the appropriate values to user.Config object as shown in this wiki article, but this needs to be done before you make a report download call.

Let me know if this doesn't resolve your issue.

Cheers,
Anash P. Oommen,
AdWords API Advisor.
Reply all
Reply to author
Forward
0 new messages