5

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!

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

4

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks so much for your help with this. Sadly, that code results in the same error message as mine at the Execute line: {"Error parsing NaN value. Path '', line 0, position 0."} – Paul Sanders Sep 16 '15 at 13:37
  • NP always fun to play with a new API :) – Linda Lawton - DaImTo Sep 16 '15 at 13:37
  • Ah, that makes sense. Webmaster tools only stores 90 days data. – Paul Sanders Sep 16 '15 at 13:42
  • It is very encouraging that you've got data! Unfortunately I am still getting the same error with recent dates. I am using a Service account and keyfile for original authorisation -- I don't know whether that would make any difference. Tried using your code instead, but the OAuth keeps using a different port number so it will never "match a registered redirect URI." – Paul Sanders Sep 16 '15 at 14:51
  • Use a native client id not a web one for testing. Service account needs to be granted access somehow I will look at it after dinner – Linda Lawton - DaImTo Sep 16 '15 at 15:00
  • Thanks so much. That got round the "doesn't match a registered redirect URI." problem. However, it lead to the same error as I got before. I might try creating a new project from scratch as my code is virtually identical to yours now (except the credentials obvs) but I get this weird error. BTW, you grant the Service account access in Webmaster Tools -- that bit worked for me, just can't get any data :) – Paul Sanders Sep 16 '15 at 15:13
  • Would you believe it? I put the same code in a new project and it ran first time. Thanks so much for your help with this. If I didn't know someone else was getting data I would have given up! – Paul Sanders Sep 16 '15 at 15:35