【发布时间】:2013-02-14 17:46:30
【问题描述】:
我正在尝试在我的 .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) Forbidden。”
我也试过下面的代码:
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 参数? 我收到错误是因为我正在使用我公司提供的密钥并使用来自的 CX 参数 使用我的 Google 帐户的自定义搜索引擎?
还有其他获取“cx”的方法吗?我们不想展示 Google AD。
非常感谢您的帮助。
【问题讨论】:
标签: c# .net google-custom-search