Offline Access AdWords Account Data for making Asp.Net Web Service

242 views
Skip to first unread message

Pravin Yadav

unread,
May 11, 2015, 7:18:22 AM5/11/15
to adwor...@googlegroups.com
Hi,

I am trying to access AdWords account data from asp.net web service.

but getting problem when consuming web service that time also need to accept account permissions from service so that time we can`t open permission page.

i need of offline Access permissions without visit permission page of authentication.

i already done same part with web application of asp.net but not offline access.

now it i want to try for scheduling of data/reports with emails.

so please reply.  


Raghavendra Soudala (AdWords API Team)

unread,
May 11, 2015, 4:23:05 PM5/11/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi Pravin,

Can you try setting access_type to offline as mentioned in our guideBy doing this it generates a refresh token for you, which you can use it to get access tokens for user so you don't have ask the user to prompt every time. 

I hope this resolves your problem if not let me know.

Cheers,
Raghavendra, AdWords API Team.

Pravin Yadav

unread,
May 12, 2015, 3:09:17 AM5/12/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi,
Yes i had used access_type=offline like below


even that it was not connect.

So any mistake of my code or any other way then please let me know.

My Service Code please Check it and tell about changes..


 [WebMethod(EnableSession = true, Description = "Authontication & Get Data From AdWords")]
        public DataTable AuthonticationConfig()
        {
            {                
                DataTable cdataTable = new DataTable();
                AdWordsUser user = new AdWordsUser();
                (user.Config as AdWordsAppConfig).ClientCustomerId = "xx3-xxx-xxxx";
                //auth_token = AdWords::AuthToken.get_token(Email,Password,'www.google.com', 443, true);
                AdWordsAppConfig config = new AdWordsAppConfig();
                if (config.OAuth2Mode == OAuth2Flow.APPLICATION &&
                    string.IsNullOrEmpty(config.OAuth2RefreshToken))
                {
                    config.OAuth2RedirectUri = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
                    
                    OAuth2ProviderForApplications oAuth = new OAuth2ProviderForApplications(config);

                    if (HttpContext.Current.Request.Params["state"] == null)
                    {
                        oAuth.State = "callback";

                        HttpContext.Current.Response.Redirect("https://accounts.google.com/o/oauth2/auth?                       scope=https%3a%2f%2fwww.googleapis.com%2fauth%2fadwords&state=callback&redirect_uri=http%3a%2f%2flocalhost%3a1366%2fOAuthLogin.aspx&response_type=code&client_id=4516991244-33c20b7an0j8m1e1gtin9mr2l9vc46vr.apps.googleusercontent.com&access_type=offline");
                        //HttpContext.Current.Response.Redirect(oAuth.GetAuthorizationUrl());
                    }
                    else if (HttpContext.Current.Request.Params["state"] == "callback")
                    {
                        oAuth.FetchAccessAndRefreshTokens(HttpContext.Current.Request.Params["code"]);

                        Session["OAuthProvider"] = oAuth;
                    }
                    // return HttpContext.Current.Session["OAuthProvider"];
                    AdWordsAppConfig config1 = (user.Config as AdWordsAppConfig);
                    if (config1.OAuth2Mode == OAuth2Flow.APPLICATION &&
                          string.IsNullOrEmpty(config1.OAuth2RefreshToken))
                    {
                        user.OAuthProvider = (OAuth2ProviderForApplications)Session["OAuthProvider"];
                    }
                    
                    string txtCustomerId = "955-757-3583";

                    string getIndex = "Campaign";

                    if (Session["OAuthProvider"] != null)
                    {
                        Session["AdwordsData"] = null;

                        #region Campaign
               Selector selector = new Selector();
                      ..............
                      .............
                     ............My Get Campaign Code
                     .............
             }
       return datatable;



Consuming  Service

 localhost.MaiaAdWordsService service = new localhost.MaiaAdWordsService();

            DataTable dt = new DataTable();

            dt = service.AuthonticationConfig();//Getting Error at here..................
            Gdv_TestService.DataSource = dt;

Please suggest solution.
  

Josh Radcliff (AdWords API Team)

unread,
May 12, 2015, 4:53:34 PM5/12/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi Pravin,

What is the error you get when you hit the line of code you highlighted?

Thanks,
Josh, AdWords API Team

Pravin Yadav

unread,
May 13, 2015, 1:05:45 AM5/13/15
to adwor...@googlegroups.com, yadav.p...@gmail.com

Hi..




Consuming  Service

 localhost.MaiaAdWordsService service = new localhost.MaiaAdWordsService();

            DataTable dt = new DataTable();

            dt = service.AuthonticationConfig();//Getting Error at here..................
            Gdv_TestService.DataSource = dt;

Getting following error at highlighted line..

The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://accounts.google.com/o/oauth2/auth?scope=https%3a%2f%2fwww.googleapis.com%2fauth%2fadwords&state=callback&redirect_uri=http%3a%2f%2flocalhost%3a25232%2fMaiaAdWordsService.asmx&response_type=code&client_id=4516991244-33c20b7an0j8m1e1gtin9mr2l9vc46vr.apps.googleusercontent.com&access_type=offline">here</a>.</h2>
</body></html>


I cant open permission page and accept Permissions at the time of service consuming.
 
  

Pravin Yadav

unread,
May 13, 2015, 1:17:19 AM5/13/15
to adwor...@googlegroups.com


On Wednesday, May 13, 2015 at 10:35:45 AM UTC+5:30, Pravin Yadav wrote:

Hi..

          In detail about errors. 
 
When i using HttpContext.Current.Response.Redirect(oAuth.GetAuthorizationUrl());    //access_type is not offline that time getting following error at same line

Anash P. Oommen (AdWords API Team)

unread,
May 14, 2015, 3:44:38 PM5/14/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi Pravin,

I think the problem is with the way you designed your service. ASMX page (or rather a WebMethod) is the wrong place to send the user through a redirect. I'm assuming that you'd need to authenticate the user AND have a web service that you can call from other applications. If yes,

- You should build a web frontend (aspx) that authenticates the user, and obtain the refresh token. Pretty much the logic in https://github.com/googleads/googleads-dotnet-lib/tree/master/examples/AdWords/CSharp/OAuth, except that Default.aspx.cs doesn't call any AdWords APi services; it simply saves the access and refresh tokens into a database.

- You should build a web service (asmx) that reads the refresh and access tokens from the database, construct an AdWordsUser and make an API call. https://github.com/googleads/googleads-dotnet-lib/blob/master/examples/AdWords/CSharp/OAuth/OAuthLogin.aspx.cs#L80 has some comments on how to reconstruct a credentials object from scratch.

- You should set RetryCount in your asmx service's web.config so that it will fetch a new access token using the refresh token in case the access token has expired. If this fails, the asmx service should throw an exception (and probably put a flag in the database) so that the UI can then ask the user to reauthorize your application the next time they visit your site.

Hope this helps. Let me know if you have more questions,

Cheers,
Anash P. Oommen,
AdWords API Advisor.

Pravin Yadav

unread,
Jun 5, 2015, 5:23:09 AM6/5/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi Anash,
               You given suggestions are useful to me.

              but now getting issue to access offline authentication without visit account permissions page make connection and retrieve data.     
              
              now i need process of storing/save the OAuth2 Provider for future use in database. i use your previous message link for (regard)offline adwords account access but i can't implement it properly.
              
              then how to save oAuth.RefreshToken,oAuth.AccessToken, oAuth.UpdateOn, Oauth.ExpiresIn or any other property value if needed for offline account access
              and what is flow of that.

           Please reply. 
 

On Friday, May 15, 2015 at 1:14:38 AM UTC+5:30, Anash P. Oommen (AdWords API Team) wrote:
Hi Pravin,

Anash P. Oommen (AdWords API Team)

unread,
Jun 8, 2015, 12:59:59 PM6/8/15
to adwor...@googlegroups.com, yadav.p...@gmail.com

Pravin Yadav

unread,
Jun 9, 2015, 8:38:16 AM6/9/15
to adwor...@googlegroups.com, yadav.p...@gmail.com

Hi Anash,
               Thanks for your & Mr. Josh's suggestions.

                finally done my web service with offline authentication.
                 
 Thank u so much.

Pravin Yadav

unread,
Jul 16, 2015, 7:35:40 AM7/16/15
to adwor...@googlegroups.com
Hi,

       I done with ASP.Net Service get data from offline adwords accounts.

       i saved oAuth.RefreshToken, oAuth.AccessToken, oAuth.UpdatedOn and oAuth.ExpiresIn in database table and used in my asp.net web service now it working properly

       But this web service i want use in scheduler it works background, so after expiring of Refresh Token and Access Token i need to go return online account permission page  but we cant redirect
     
      account permission page during ASP.Net web service or scheduler(because service run in background) then how it possible to regenerate tokens and save it to database table.


      (without online visit of permissions page of google adwords account).

      Please reply.

  Thanks & Regards,

  Pravin Y.                                           






                                                                                                                                                                                                       




    

Anash P. Oommen (AdWords API Team)

unread,
Jul 20, 2015, 2:58:25 PM7/20/15
to adwor...@googlegroups.com, yadav.p...@gmail.com
Hi Pravin,

You can setup the client library to refresh the access token by itself (without any user interaction), see https://github.com/googleads/googleads-dotnet-lib/wiki/Using-OAuth2#dealing-with-invalid-oauth2-tokens for instructions.

Cheers,
Anash

Pravin Yadav

unread,
Aug 5, 2015, 3:25:39 AM8/5/15
to AdWords API Forum, yadav.p...@gmail.com
Hi Anash,
 
               I am very thankful to u and your team to give me so much support regards AdWords Account implement,Manage & data fetch online and offline data.
              
               Your number of suggestions are useful.
Reply all
Reply to author
Forward
0 new messages