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

前端 未结 5 892
执笔经年
执笔经年 2021-01-01 00:49

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,

相关标签:
5条回答
  • 2021-01-01 01:30

    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);
    }
    
    0 讨论(0)
  • 2021-01-01 01:40

    look at API Reference using 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 off of webClient or HttpRequest, as you wish

    0 讨论(0)
  • 2021-01-01 01:45

    you may start from Getting Started with the API.

    0 讨论(0)
  • 2021-01-01 01:50

    First of all, you need to make sure you've generated your API Key and the CX. I am assuming you've done that already, otherwise you can do it at those locations:

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

    Once you have those, here is a simple console app that performs the search and dumps all the titles/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'm using a generic JSON deserialization into a dictionary instead of being strongly-typed. This is for convenience purposes, since I don't want to create a class that implements the search results schema. With this approach, the payload is the nested set of key-value pairs. What interests you most is the items collection, which is the search result (first page, I presume). I am only accessing the "title" and "link" properties, but there are many more than you can either see from the documentation or inspect in the debugger.

    0 讨论(0)
  • 2021-01-01 01:51

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

    0 讨论(0)
提交回复
热议问题