Search code examples
c#.netgoogle-apigoogle-api-dotnet-clientgoogle-api-webmasters

.Net struggles with Webmaster Tools (Google.Apis.Webmasters.v3 Nuget package)


I wonder if anyone has got any further than me using the new Search Analytics functions of the Google Webmaster Tools API via .Net?

I am using the Google.Apis.Webmasters.v3 Nuget package and have got as far as authenticating and connecting (using a Service Account)

However I'm struggling to get anywhere with Search Analytics.

I couldn't find any code samples online so have been guided by the Class info at https://developers.google.com/resources/api-libraries/documentation/webmasters/v3/csharp/latest/annotated.html and a lot of guesswork.

Here is the code I am using:

SearchanalyticsResource mySearchanalyticsResource = new SearchanalyticsResource(service);

SearchAnalyticsQueryRequest myRequest = new SearchAnalyticsQueryRequest(); 
myRequest.StartDate = "2015-08-01"; 
myRequest.EndDate = "2015-08-31"; 
myRequest.RowLimit = 10;

SearchanalyticsResource.QueryRequest myQueryRequest = mySearchanalyticsResource.Query(myRequest, site.SiteUrl); 
SearchAnalyticsQueryResponse myQueryResponse = myQueryRequest.Execute();

It runs OK until the Execute method when I get "An Error occurred, but the error response could not be deserialized". Exception detail below...

Newtonsoft.Json.JsonReaderException {"Error parsing NaN value. Path '', line 0, position 0."}

Any help or code samples would be very gratefully received!


Solution

  • This compiles but its not returning any data for me.

    Auth:

     public static WebmastersService WMAuthenticateOauth(string clientId, string clientSecret, string userName)
            {
    
                string[] scopes = new string[] { WebmastersService.Scope.Webmasters };     // View analytics data
    
                try
                {
                    // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                    UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                                 , scopes
                                                                                                 , userName
                                                                                                 , CancellationToken.None
                                                                                                 , new FileDataStore(".", true)).Result;
    
                    WebmastersService service = new WebmastersService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "WebMasters API Sample",
                    });
                    return service;
                }
                catch (Exception ex)
                {
    
                    Console.WriteLine(ex.InnerException);
                    return null;
    
                }
    
            }
    

    Request

    var service = Authentcation.WMAuthenticateOauth(clientid, secret, "testmmm");
    
    
       IList<string> newlist = new List<string> ();
       newlist.Add("country");
       newlist.Add("device");
    
       SearchAnalyticsQueryRequest body = new SearchAnalyticsQueryRequest();
       body.StartDate = "2015-04-01";
       body.EndDate = "2015-05-01";
       body.Dimensions = newlist;
    
       var result = service.Searchanalytics.Query(body, "http://www.daimto.com/").Execute();
    

    I have also tried testing using the try me at the bottom of this page. It doesn't return anything either.

    Strange API this.

    Update:

    I finally got data back I set the dates to

    body.StartDate = "2015-09-01";
    body.EndDate = "2015-09-15";

    I wonder if this thing has limited data, it only goes back so far.

    enter image description here