消息[无效值]位置[ - ]原因[无效]域[全局]

时间:2015-06-20 16:13:52

标签: c# google-api google-custom-search

我使用Google自定义搜索API已有几年了。此代码适用于Google nuget package" Google.Apis.Customsearch.v1客户端库" (v1.9.0.460)。

这是我的c#代码:

public Search SearchNow(string query)
{
    CustomsearchService service = new CustomsearchService(
        new BaseClientService.Initializer()
        {
            ApiKey = Properties.Resources.Google_Key,
            ApplicationName = "my-app-name"
        });

    CseResource.ListRequest req = service.Cse.List(query);
    req.Cx = Properties.Resources.Google_CX;
    req.Start = 1;

    return req.Execute();
}

此代码在我的服务器上运行超过一年没有任何问题。从上周开始,我开始回到这个例外:

  

服务customsearch引发了异常:   Google.GoogleApiException:Google.Apis.Requests.RequestError无效

     

值[400]错误

     

[消息[无效值]位置[ - ]原因[无效]域[全局]]

     

在Google.Apis.Requests.ClientServiceRequest`1.Execute()中   C:\代码\ google.com \谷歌API-DOTNET客户端\ DEFAULT \工具\ Google.Apis.Release \ BIN \调试\测试\ DEFAULT \ SRC \ GoogleApis \蜜蜂\请求\ ClientServiceRequest.cs:行   96#at##。###。cs:line中的SearchNow(字符串查询,Int64 startResult)   144在Tests.Program.Main()在*** \ Tests \ Program.cs:第67行

我不知道改变了什么,为什么这停止了工作。

我做了一个小测试 - 发送一个" Hello world"搜索查询到谷歌 - 这是第一次工作。我运行这个测试agian和agian,发现这个代码在70%的时间内运行良好,有时它失败了(上面提到的错误)。

我双重验证了这些参数:ApiKey,ApplicationName,CX。

那么,可能是什么问题?

3 个答案:

答案 0 :(得分:3)

问题似乎在于:

req.Start = 1;

从我的代码中删除它可以解决我的问题。

答案 1 :(得分:0)

如果在查询CSE JSON API时将 start 参数设置为大于100的数字,则会发生此错误。结果数限制为最大。 100个结果。

从Google CSE文档中:

  

开始:要返回的第一个结果的索引。每页的默认结果数是10,因此&start = 11将从第二页结果的顶部开始。注意: JSON API绝不会返回超过100个结果,即使与查询匹配的文档超过100个,因此将start + num的总和设置为大于100的数字将产生一个错误。另请注意,num的最大值为10。

https://developers.google.com/custom-search/v1/cse/list

解决方案:始终将 start 参数设置为数字<= 100。

答案 2 :(得分:0)

这可能也有帮助。 我遇到了这个错误:

  

Google.GoogleApiException:'Google.Apis.Requests.RequestError无效   资源ID值。 [400]错误[消息[无效的资源ID值。]   位置[-]原因[无效]域[全局]

原因是将EventID设置为长度小于5的字符串。
在类中使用:

修复
sEventID = value.PadLeft(5, '0');

Event.id reference