.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を表示したくありません。
よろしくお願いします。