Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Does anyone have any experience with Google.Apis.Webmasters.v3 (Nuget[^])?

I am having very little success with it >_<<br mode="hold" />
I want to have a stand alone app on my server that pulls the "impressions" fromn webmaster.Searchanalytics. I cannot get this to authenticate unless the windows user is logged into a google account with access (I would like it to authenticate itself), and when I am logged in I get nothing back. Just an ETag, a ResponseAggregationType and null Rows.

Most people appear to either not use the .net API, mostly because they are writing python or php, or are not using webmaster tools which appear to have a different level of authentication required.

I have posted on Googles forums but I doubt I'll see a response any tome soon.




So - Here is my code (If you wish to test then you will need to set up an api account and use a website you claim ownership of, i think )

C#
using Google.Apis.Services;
using Google.Apis.Discovery.v1;
using Google.Apis.Discovery.v1.Data;
using Google.Apis.Webmasters.v3;
using Google.Apis.Webmasters.v3.Data;
using Google.Apis.Auth.OAuth2;
//....

 public static WebmastersService AuthenicateService(string userName)
        {
            ClientSecrets secrets = new ClientSecrets
            {
                ClientId = OAuthClientId,
                ClientSecret = OAuthClientSecret
            };
            string[] scopes = { WebmastersService.Scope.Webmasters };

            try
            {
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    secrets,
                    scopes,
                    Username,
                    new CancellationToken(false)
                    ).Result;

                var service = new WebmastersService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "ApiCall Sample",
                });
                return service;
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToVerbose());
                return null;
            }
        }
        public static async Task Run()
        {
            var service = AuthenicateService(Username);
            foreach (var row in service.Features)
            {
                Console.WriteLine(row);
            }
            var query = service.Searchanalytics.Query(
                new SearchAnalyticsQueryRequest
                {
                    SearchType = "web",
                    //Dimensions = new[] {
                    //      "query"
                    //     },
                    StartDate = DateTime.Now.Date.ToString("yyyy-MM-dd"),
                    EndDate = DateTime.Now.AddDays(1).Date.ToString("yyyy-MM-dd")
                }, SiteUrl);
            try
            {

                var response = await query.ExecuteAsync();
                if (response.Rows != null)
                    foreach (ApiDataRow row in response.Rows)
                    {
                        Console.WriteLine(row.Impressions);
                    }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToVerbose());
            }
        }
Posted
Comments
anu katari 8-Jun-17 23:18pm    
Any leads on this please ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900