Click here to Skip to main content
14,383,744 members
Rate this:
Please Sign up or sign in to vote.
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 )

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100