Tag: 谷歌定制搜索

在.NET中使用Google自定义搜索API的步骤

我想在我的.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(); […]