How to Integrate Google Search Console API in Asp.Net Core using C#



Google Search Console (GSC) is a powerful tool for website owners and developers to monitor their website's performance in search engines. It provides valuable insights into how users interact with your website, including keywords they use to find it, the pages they visit, and the devices they use. In this blog post, we will explore how to integrate GSC API in Asp.Net Core using C#.




Before we begin, you need to have a Google Search Console account and a website that you want to monitor. You also need to have the following tools installed on your computer:


* Visual Studio (or any other IDE of your choice)

* .NET Core SDK

* Newtonsoft.Json NuGet package


Step 1: Create a new Asp.Net Core project


Open Visual Studio and create a new Asp.Net Core project. You can do this by selecting "Create a new project" from the start menu, then selecting "ASP.NET Core Web Application" as the project template.


Step 2: Install Newtonsoft.Json NuGet package


Once you have created your project, open the Package Manager Console in Visual Studio and run the following command to install the Newtonsoft.Json NuGet package:

Install-Package Newtonsoft.Json

This package will help us parse JSON data returned by the GSC API.


Step 3: Get an access token for the GSC API


To use the GSC API, you need to obtain an access token. You can do this by following these steps:


1. Go to the Google Cloud Console (<>) and create a new project or select an existing one.

2. In the left-hand menu, click on "APIs & Services" > "Credentials".

3. Click on "Create Credentials" and select "OAuth client ID".

4. Select "Web application" as the application type and enter a name for your application.

5. Click on "Create" to create the OAuth client ID.

6. Note down the Client ID and Client Secret. You will use them later in your code.


Step 4: Write the code to integrate GSC API in Asp.Net Core


Now that you have obtained an access token, it's time to write the code to integrate GSC API in Asp.Net Core. Here is a sample code that demonstrates how to do this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net.Http;

using System.Threading.Tasks;

using Newtonsoft.Json;

namespace GSCAPIIntegration.Controllers




public class SearchConsoleController : ControllerBase


private readonly HttpClient _httpClient;

private readonly string _clientId;

private readonly string _clientSecret;

public SearchConsoleController(HttpClient httpClient, string clientId, string clientSecret)


_httpClient = httpClient;

_clientId = clientId;

_clientSecret = clientSecret;



public async Task Search(string query)


var authHeader = await GetAuthHeader();

var response = await _httpClient.GetAsync($"{query}&cx=YOUR_SEARCH_ENGINE_ID", new HttpRequestMessage() { Headers = authHeader });

if (response.IsSuccessStatusCode)


var responseBody = await response.Content.ReadAsStringAsync();

var searchResults = JsonConvert.DeserializeObject(responseBody);

return Ok(searchResults);




return BadRequest("Failed to fetch search results.");



private async Task GetAuthHeader()


var authHeader = new HttpHeaders();

authHeader.Authorization = new AuthenticationHeaderValue("Bearer", await GetAccessToken());

return authHeader;


private async Task GetAccessToken()


var clientCredential = new ClientCredential(_clientId, _clientSecret);

var tokenResponse = await GoogleWebAuthorizationBroker.RequestAsync(GoogleClientSecrets.Load("client_secrets.json").Secrets,

new[] { "" }, clientCredential, CancellationToken.None);

return tokenResponse.AccessToken;


public class SearchResponse


public List Results { get; set; }

public class SearchResult


public string Title { get; set; }

public string Link { get; set; }

public string Snippet { get; set; }



In this code, we have created a new controller called `SearchConsoleController`. This controller has a single action method called `Search`, which takes a query parameter and returns the search results for that query.


To authenticate with the GSC API, we use the OAuth 2.0 protocol. We first obtain an access token by calling the `GetAccessToken` method, which uses the Google Web Authorization Broker to request an access token from the Google OAuth server. Once we have obtained the access token, we include it in the `Authorization` header of all HTTP requests to the GSC API.


The `Search` action method sends a GET request to the GSC API with the query parameter and the access token included in the `Authorization` header. The response from the API is deserialized into a `SearchResponse` object, which contains a list of `SearchResult` objects. These objects represent the search results for the given query.


Step 5: Run the application


Now that you have written the code to integrate GSC API in Asp.Net Core, it's time to run your application. Open Visual Studio and select "Start" from the top menu. This will start your application on the default URL (<http://localhost:5000>).


You can now test your application by sending a GET request to the `/search` endpoint with a query parameter. For example, you can send a request to `http://localhost:5000/search? core` to get search results for the phrase " core".




In this blog post, we have explored how to integrate GSC API in Asp.Net Core using C#. We have created a new controller that sends HTTP requests to the GSC API and deserializes the response into a `SearchResponse` object. We have also shown you how to obtain an access token for the GSC API using OAuth 2.0 protocol.


By following these steps, you can easily monitor your website's performance in search engines and get valuable insights into how users interact with your website.


© 2024 - ErnesTech - Privacy
E-Commerce Return Policy