Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a search with Google Custom Search API for .NET?

Tags:

google-api

api

I just discovered the Google APIs Client Library for .NET, but because of lack of documentation I have a hard time to figure it out.

I am trying to do a simple test, by doing a custom search, and I have looked among other, at the following namespace:

Google.Apis.Customsearch.v1.Data.Query

I have tried to create a query object and fill out SearchTerms, but how can I fetch results from that query?

like image 686
Dofs Avatar asked Nov 07 '11 18:11

Dofs


1 Answers

My bad, my first answer was not using the Google APIs.

As a pre-requisite, you need to get the Google API client library

(In particular, you will need to reference Google.Apis.dll in your project). Now, assuming you've got your API key and the CX, here is the same code that gets the results, but now using the actual APIs:

string apiKey = "YOUR KEY HERE";
string cx = "YOUR CX HERE";
string query = "YOUR SEARCH HERE";

Google.Apis.Customsearch.v1.CustomsearchService svc = new Google.Apis.Customsearch.v1.CustomsearchService();
svc.Key = apiKey;

Google.Apis.Customsearch.v1.CseResource.ListRequest listRequest = svc.Cse.List(query);
listRequest.Cx = cx;
Google.Apis.Customsearch.v1.Data.Search search = listRequest.Fetch();

foreach (Google.Apis.Customsearch.v1.Data.Result result in search.Items)
{
    Console.WriteLine("Title: {0}", result.Title);
    Console.WriteLine("Link: {0}", result.Link);
}
like image 195
David Airapetyan Avatar answered Nov 15 '22 10:11

David Airapetyan