How to get search analytics from webmaster tools api using C#

In this blog post, we are gonna retrieve search analytics from webmaster tools API using C#.

Let’s get started by creating a  .NET application with framework 4.0 or 4.5. And install the Google Webmaster tools API package from NuGet Package Manager. After installing the package, get ClientId and ClientSecret from Google Developer Console.  To get ClientId and Client Secret from Google developer console follow this blog post.

Once you get Client ID and Client Secret, You will have to authenticate OAuth credentials which are used in the application. Here is the snippet for that:

public static WebmastersService AuthenticateOauth(string clientId, string clientSecret, string userName)


string[] scopes = new string[] { WebmastersService.Scope.Webmasters};



UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }, scopes, userName, CancellationToken.None).Result;

WebmastersService service = new WebmastersService(new BaseClientService.Initializer(){

HttpClientInitializer = credential,

ApplicationName = “WebMasters API Example”,


return service;


catch( Exception ex){

Console.WriteLine( ex.InnerException);

return null;





Now to authenticate, you would call the method you defined above like this:

var Service = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "TEST");

Now after successful authentication, we can query over search analytics with filters and parameters we want. We would have either zero or more rows of data grouped by row keys (dimensions) that we define. Here is the sample query:

 IList newlist = new List ();
 SearchAnalyticsQueryRequest body = new SearchAnalyticsQueryRequest();
 body.StartDate = "2016-09-01";
 body.EndDate = "2016-11-15";
 body.Dimensions = newlist;    
 var result = service.Searchanalytics.Query(body, "").Execute();


The output is as follows:

"keys": [
   "clicks": 785,
   "impressions": 20011,
   "ctr": 0.02865020895265176,
   "position": 9.619318426608073


Here is how you can get search analytics from webmaster tools API using C#.

Thanks for dropping by !! Feel free to comment to this post or you can also reach me at [email protected]