Message[Invalid Value] Location[ - ] Reason[invalid] Domain[global]

7.8k views Asked by At

I'm using Google custom search API for few years. This code is working with Google nuget package "Google.Apis.Customsearch.v1 Client Library" (v1.9.0.460).

This is my c# code:

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();
}

This code is running on my server for more then a year without any problem. Since last week I'm started to get back this exception:

The service customsearch has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError Invalid

Value [400] Errors

[Message[Invalid Value] Location[ - ] Reason[invalid] Domain[global] ]

at Google.Apis.Requests.ClientServiceRequest`1.Execute() in 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 at ###.SearchNow(String query, Int64 startResult) in ###.cs:line 144 at Tests.Program.Main() in ***\Tests\Program.cs:line 67

I'm have no clue what is changed \ why this stopped working.

I'm made up a small test - sending a "Hello world" search query to google - This was working on the first time. I'm run this test agian and agian and found that this code is working good in 70% of times, other times it fails (with error mentioned above).

I'm double verified those params: ApiKey, ApplicationName, CX.

So, what could be the problem?

3

There are 3 answers

0
K. Suter On

This error can occur if you set the start parameter to a number higher than 100 when querying the CSE JSON API. The number of results is restricted to max. 100 results.

From the Google CSE documentation:

start: The index of the first result to return. The default number of results per page is 10, so &start=11 would start at the top of the second page of results. Note: The JSON API will never return more than 100 results, even if more than 100 documents match the query, so setting the sum of start + num to a number greater than 100 will produce an error. Also note that the maximum value for num is 10.

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

Solution: Set the start parameter always to a number <= 100.

0
Chris Catignani On

This may help too.... I had gotten this error:

Google.GoogleApiException: 'Google.Apis.Requests.RequestError Invalid resource id value. [400] Errors [Message[Invalid resource id value.] Location[ - ] Reason[invalid] Domain[global]

And the cause was setting the EventID to a string who length was shorter than 5.
Fixed in the class with :

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

Event.id reference

1
Mike Rouse On

The problem appears to be with:

req.Start = 1;

Removing this from my code fixes the problem for me.