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?
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:
https://developers.google.com/custom-search/v1/cse/list
Solution: Set the start parameter always to a number <= 100.