8

.NETプロジェクトでGoogleカスタム検索APIを使用しようとしています。会社から提供されたAPIキーを持っています。Googleアカウントを使用してカスタム検索エンジンを作成し、「cx」値をコピーしました。

私は次のコードを使用しています:

string apiKey = "My company Key";
string cx = "Cx";
string query = tbSearch.Text;

WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Only a test!");

string result = webClient.DownloadString(String.Format("https://www.googleapis.com/customsearch/v1?key={0}&cx={1}&q={2}&alt=json", apiKey, cx, query));

次のエラーが発生します:「リモートサーバーがエラーを返しました:(403)禁止されています。」

私も次のコードを試しました:

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 result1 in search.Items)
{
   Console.WriteLine("Title: {0}", result1.Title);
   Console.WriteLine("Link: {0}", result1.Link);
}

ここで、Fetch()で次の例外が発生します。

Google.Apis.Requests.RequestErrorアクセス​​が構成されていません[403]エラー[メッセージ[アクセスが構成されていません]場所[-]理由[accessNotConfigured]ドメイン[usageLimits]

CXパラメータは必要ですか?会社から提供されたキーを使用していて、Googleアカウントを使用してカスタム検索エンジンのCXパラメーターを使用しているため、エラーが発生しますか?

'cx'を取得する他の方法はありますか?GoogleADを表示したくありません。

よろしくお願いします。

4

3 に答える 3

14

あなたがまだこれに興味があるかどうかはわかりません。

広告なしで結果を得るには、料金を支払う必要があります。 情報 @ Google

はい、検索に使用する Google カスタム検索エンジンを指定する cx が必要です。このGoogleページからカスタム検索エンジンを作成できます

現在の API バージョン 1.3.0-beta の検索結果を取得するための現在のコードは次のとおりです。

        string apiKey = "Your api key";
        string cx = "Your custom search engine id";
        string query = "Your query";

        var svc = new Google.Apis.Customsearch.v1.CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey });
        var listRequest = svc.Cse.List(query);

        listRequest.Cx = cx;
        var search = listRequest.Fetch();

        foreach (var result in search.Items)
        {
            Response.Output.WriteLine("Title: {0}", result.Title);
            Response.Output.WriteLine("Link: {0}", result.Link);
        }

お役に立てれば

于 2013-05-01T17:36:33.953 に答える