WebMaster Tools API with C# 7

webmaster tools tweetYesterday I was mentioned in a tweet by Paul Sandars.

@LindaLawtonDK Do you plan to do one of your fab tutorials on the new Search Analytics API for Webmasters? I’m stuck! http://goo.gl/4usHJK

fab tutorials really?   I do get emails now and then and the site generates a handful of comments every week.  However I often wonder how many people I help.      I am on twitter and Google+ if you like what i am doing feel let me know I would really appreciate it.     Heck even if you don’t like what I am doing let me know and maybe I can fix what ever it is you think is wrong.

So by request.    I give you

WebMaster Tools API with C#


Create a visual studio project .net framework 4.0 or 4.5.   Import the following Nuget Package

PM> Install-Package Google.Apis.Webmasters.v3

Create either an OAuth2 or a service account credentials on Google Developer console.


I have included both the Service account and the Oauth2 authentication methods below.

To call the Oauth2 method you would do something like this. Remember CLIENT_ID and CLIENT_SECRET can be found on the Google Developers console.

 var service = Authentication.AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "TEST");

To call the service account you will need the serviceAccountEmail and the path to the keyFilePath both values are found on the Google developers console.

var service = Authentication.AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath)

As always with service accounts they need to be pre-authorized to be able to access the data. We do that on the webMaster Tools website.

WebMaster Service AccountP ermissions go to users and property owners and add the service account email address that you found on the Google Developers console as a user.


Requesting data

One thing that Paul and I found while we where trying to get this working was that, the web master tools website only stores data for 90 days. Dont bother requesting data that’s over 90 days old its probably not there. However if you find that it is there please let me know. I hate the idea of data being deleted.

Paul was working with the Search Analytics: query

Query your search traffic data with filters and parameters that you define. The method returns zero or more rows grouped by the row keys (dimensions) that you define.

 IList newlist = new List ();
 SearchAnalyticsQueryRequest body = new SearchAnalyticsQueryRequest();
 body.StartDate = "2015-09-01";
 body.EndDate = "2015-09-15";
 body.Dimensions = newlist;    
 var result = service.Searchanalytics.Query(body, "https://www.daimto.com/").Execute();

Raw results:

   "keys": [
   "clicks": 665,
   "impressions": 23211,
   "ctr": 0.02865020895265176,
   "position": 9.619318426608073


Google WebMaster tools API returns some analytical data that Google Analytics doesn’t have. If it is true that the webmaster tools API does delete data over 90 days old I think it would be a good idea to export as much of this information as possible before it is lost for ever.   Search Analytics could be very valuable.

The question on stackOverflow can be found here .Net struggles with Webmaster Tools (Google.Apis.Webmasters.v3 Nuget package) Paul apparently really likes long stack question titles.

I also have a sample project that I created ages ago for working with the WebMaster Tools API I will try and update it asap to add this. Google-dotnet-samples

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

7 thoughts on “WebMaster Tools API with C#

  • devin

    Hi Linda,

    I’ve tried your example as a service account:

    var service = AuthenticateServiceAccount(ServiceAccountEmail, KeyFilePath);

    Then tried pulling back crawl error counts from our website using
    var whatSiteUrl = “http://www.kronos.com/”;
    var request = service.Urlcrawlerrorscounts.Query(whatSiteUrl).Execute();

    and I keep getting this bizarre error:
    “An Error occurred, but the error response could not be deserialized”

    Have you seen this error? I’ve also run into this error while executing:
    var result = service.Searchanalytics.Query(body, whatSiteUrl).Execute();

  • shwetha

    Hi Linda

    i tried this example every thing worked fine except this line service.Searchanalytics.Query(body, “siteurl”).Execute();
    body.StartDate = “2016-02-01”;
    body.EndDate = “2016-02-09”;
    body.Dimensions = dimensionlist;

    body.SearchType = “web”;
    body.RowLimit = 20;
    body.AggregationType = “auto”;
    var result = service.Searchanalytics.Query(body, “siteurl”).Execute();
    this line is not returning any data and hence i am getting the error
    “An Error occurred, but the error response could not be deserialized”

    • Linda Lawton Post author

      siteUrl string The URI of the property as defined in Search Console. Examples: http://www.example.com/ or android-app://com.example/

      var result = service.Searchanalytics.Query(body, “siteurl”).Execute();

      You are sending the string “siteurl” Are you sure you dont mean

      var result = service.Searchanalytics.Query(body, siteurl).Execute();

      or maybe

      var result = service.Searchanalytics.Query(body, "http://www.example.com/").Execute();

      However the error message “An Error occurred, but the error response could not be deserialized” sounds more like an issue with the client library I will have to test the method.

      • shwetha

        Hi Linda

        i am sensing the siteurl directly
        simillar to

        var result = service.Searchanalytics.Query(body, “http://www.example.com/”).Execute();

  • Faizan Manzoor

    The above code only generate Google search console Performance report in which we have clicks, CTR , Impression and position against different dimension like Country, Page, Query and Date wise.

    But what about other reports how we get other reports like Crawl Stats, Error report and mobile friendly test.