why does your call to the google search api fail?

The query is not being send to google. To fix your code you need to tell the api what query to use. After listRequest.Cx = searchEngineId; add listRequest.Q = query;

var count = 0;
string apiKey = "THE API KEY";
string searchEngineId = "THE SEARCH ENGIN ID";
string query = "rent amazon movie Will Penny";

var customSearchService = new CustomsearchService(new BaseClientService.Initializer
{
    ApiKey = apiKey
});

CseResource.ListRequest listRequest = customSearchService.Cse.List();
listRequest.Cx = searchEngineId;
listRequest.Q = query; // <---- Add this line

List<string> linksReturned = new List<string>();

while (count < 10) // Google limit you to 100 records
{
    listRequest.Start = count * 10;
    var paging = listRequest.Execute().Items; 
    
    foreach (var item in paging)
    {
        linksReturned.Add("Title : " + item.Title + Environment.NewLine + "Link : " +
                          item.Link +
                          Environment.NewLine + Environment.NewLine);
    }

    count++;
}

In your code you have a comment that you don’t know what var count = 0; is for. It is to keep track on how many items you have requested.

If you look at google’s documentation you will see that they will only return 100 results max. After that they will give you a error. That error will also be the same generic message: “INVALID_ARGUMENT”

You can review the custom search api requirements here: https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list

The searchEngineId variable is the search Engine id that you generate on the site https://www.google.com/cse/all. The documentation you followed is a bit out of date. you will find the id here:

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top