2011-11-07 10 views
6

私はちょうどGoogle APIs Client Library for .NETを発見しましたが、ドキュメントの不足のために私はそれを理解するのが難しいです。Googleカスタム検索API for .NETで検索するにはどうすればよいですか?

私はカスタム検索を行うことによって、簡単なテストを実行しようとしていて、私は次の名前空間で、他の中で見ている:私は、クエリオブジェクトを作成してのSearchTermsを記入しようとしてい

Google.Apis.Customsearch.v1.Data.Query 

そのクエリからどのように結果を取得できますか?あなたはすべての

答えて

3

外観APIキーとCXが生成されていることを確認する必要があります。私は、そうでない場合は、あなたがこれらの場所でそれを行うことができます、あなたはすでにそれをやったと仮定しています:

  • API Key(あなたが新しいブラウザキーを作成する必要があります)
  • CXを(カスタム検索エンジンを作成する必要があります)

あなたがそれらを持っていたら、ここに検索を実行し、すべてのタイトル/リンクをダンプする簡単なコンソールアプリケーションです:

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(); 
    } 
} 

として、あなたは強く型付けされているのではなく、汎用JSON逆シリアル化を辞書に使用しています。これは、検索結果スキーマを実装するクラスを作成したくないため、便宜上のものです。このアプローチでは、ペイロードはキーと値のペアのネストされたセットです。最も興味のあるのは、検索結果(最初のページ、私が推測する)であるアイテムコレクションです。私は "タイトル"と "リンク"プロパティにのみアクセスしていますが、ドキュメンテーションやデバッガで調べること以上に多くのことがあります。

+0

"APIリファレンス"リンクが無効です... – billy

4

ファーストを望むように、あなたもwebClientまたはHttpRequestのオフに読み取ることができるストリームと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); 

または

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); 
} 

からのコードを使用してAPI Reference

+1

[Google APIクライアントライブラリfor .NET]の使用方法を探しています(http://code.google.com/p/google-api-dotnet-クライアント/ wiki/API#CustomSearch_API)を適切に使用してください。とにかく高すぎる..これも役立ちます。 – Darshana

6

私の悪いことに、私の最初の答えはGoogle APIを使用していませんでした。前提条件として

、あなたは(特に、プロジェクトでGoogle.Apis.dllを参照する必要があります)Google API client library

を取得する必要があります。さて、あなたはあなたのAPIキーとCXを持っていると仮定すると、ここで結果を取得し、同じコードがあるが、今、実際のAPI使用:

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); 
} 
関連する問題