How can I do a search with the Google UI API for .NET?

I just discovered the Google APIs Client Library for .NET , but due to the lack of documentation, it's hard for me to find it.

I am trying to run a simple test by doing a custom search, and I looked among others in the following namespace:

Google.Apis.Customsearch.v1.Data.Query 

I tried to create a query object and populate SearchTerms, but how can I get the results from this query?

+6
source share
5 answers

see the API Reference using the code from google-api-dotnet-client

 CustomsearchService svc = new CustomsearchService(); string json = File.ReadAllText("jsonfile",Encoding.UTF8); Search googleRes = null; ISerializer des = new NewtonsoftJsonSerializer(); googleRes = des.Deserialize<Search>(json); 

or

 CustomsearchService svc = new CustomsearchService(); Search googleRes = null; ISerializer des = new NewtonsoftJsonSerializer(); using (var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { googleRes = des.Deserialize<Search>(fileStream); } 

with the stream you can also read webClient or HttpRequest webClient you want

+3
source

My bad, my first answer did not use the Google API.

As a prerequisite, you need to get the Google API client library

(In particular, you will need to reference the Google.Apis.dll file in your project). Now, assuming you have an API key and CX, here is the same code that gets the results, but now uses 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); } 
+6
source

First of all, you need to make sure that you have created your API key and CX. I assume that you have already done this, otherwise you can do it in these places:

  • API key (you need to create a new browser key)
  • CX (you need to create a custom search engine)

Once you have it, here is just a console application that searches and downloads all the headers / links:

 static void Main(string[] args) { WebClient webClient = new WebClient(); string apiKey = "YOUR KEY HERE"; string cx = "YOUR CX HERE"; string query = "YOUR SEARCH HERE"; string result = webClient.DownloadString(String.Format("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}&alt=json", apiKey, cx, query)); JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string, object> collection = serializer.Deserialize<Dictionary<string, object>>(result); foreach (Dictionary<string, object> item in (IEnumerable)collection["items"]) { Console.WriteLine("Title: {0}", item["title"]); Console.WriteLine("Link: {0}", item["link"]); Console.WriteLine(); } } 

As you can see, I use universal JSON deserialization in the dictionary instead of strict input. This is for convenience, since I do not want to create a class that implements the search result schema. With this approach, the payload is an embedded set of key-value pairs. You are most interested in the collection of items that results from the search (first page, I suppose). I get access only to the "title" and "link" properties, but they can be seen much more than you can see from the documentation or check in the debugger.

+4
source

Google.Apis.Customsearch.v1 Client Library http://www.nuget.org/packages/Google.Apis.Customsearch.v1/

+1
source

You can start by getting started with the API .

0
source

Source: https://habr.com/ru/post/900966/


All Articles