【问题标题】:Message[Invalid Value] Location[ - ] Reason[invalid] Domain[global]消息[无效值]位置[-]原因[无效]域[全局]
【发布时间】:2015-06-20 16:13:51
【问题描述】:

我使用 Google 自定义搜索 API 已有几年了。此代码适用于 Google nuget 包“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:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Requests\ClientServiceRequest.cs:line 96 在 ###.SearchNow(String query, Int64 startResult) in ###.cs:line ***\Tests\Program.cs:line 67 中的 Tests.Program.Main() 中的 144

我不知道发生了什么变化\为什么它停止工作。

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

我对这些参数进行了双重验证:ApiKey、ApplicationName、CX。

那么,可能是什么问题?

【问题讨论】:

  • 嗨 - 你有想过这个吗?它刚刚开始发生在我身上!

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


【解决方案1】:

问题似乎出在:

req.Start = 1;

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

【讨论】:

  • 对我来说不完全是这个,而是 Q 请求参数。我假设这个错误只是意味着一些请求参数是错误的。
【解决方案2】:

如果您在查询 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 参数设置为数字

【讨论】:

    【解决方案3】:

    这也可能有帮助.... 我收到了这个错误:

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

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

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

    Event.id reference

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2023-02-14
      • 2023-03-11
      • 2018-12-18
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多