Google API Customsearch. I'm Getting the Error [400]: "Request contains an invalid argument." (C#)

KasunLee

New member
Joined
Oct 3, 2022
Messages
3
Location
Srilanka
Programming Experience
1-3
Hi!

I'm trying to make an app to download images from Google search queries. And I get the Error [400]: "Request contains an invalid argument." at listRequest.Execute().Items.

Current code:

C#:
using Google.Apis.Customsearch.v1.Data;

using Google.Apis.Customsearch.v1;

using Google.Apis.Services;

using System.Net;

......

......

private void Form1_Load(object sender, EventArgs e){

DownloadGoogleImages("fox mulder", 20, "C:\\", [my apikey], [my Search Engine ID]);

}

public static void DownloadGoogleImages(string query, int numResults, string saveFolderPath, string apiKey, string searchEngineId)

{

var customSearchService =

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

var listRequest = customSearchService.Cse.List();

listRequest.Cx = searchEngineId;

listRequest.SearchType = CseResource.ListRequest.SearchTypeEnum.Image;

listRequest.Num = numResults;

listRequest.Start = 1;

listRequest.Q = query;

IList<Result> paging = listRequest.Execute().Items; // RUNTIME ERROR HERE

..........

..........

What I'm doing wrong please?

Thanks.
 
This is a Google API issue, not a C# issue. What does the documentation for the API say?
 
This is a Google API issue, not a C# issue. What does the documentation for the API say?
(Sorry, I could not figure out where to post this question, so I posted it in the General section.)

Actually i could not find any C#+Google API reference anywhere for this specific error. The search results I found were pretty much all on Python errors and not related to Class customSearchService. I dunno what to do. I don't understand what Invalid Argument I'm passing to customSearchService.
 
How did you figure out what parameters in the first place? Please tell me you were not just using IntelliSense and just guessing.
 
How did you figure out what parameters in the first place? Please tell me you were not just using IntelliSense and just guessing.

Well I've done this sometime ago (and it worked), but the source code got lost. I wrote this from memory of that old code. I did the old code with the help of some guide on the net those days, but I can't find the source anymore.
 
My suggestion is to run some kind of network trace to see what parameters are actually sent when running your code and you get an error. Then go to Google's Custom Search API documentation page where they let you call the API straight from the browser and try the same set of parameters. Run a network trace there to. If that web based call fails, then it's your set of parameters. Of the web based version succeeds, then compare the network traces to see the difference between your code and the web based one.
 
Use a query tool like Insomnia in conjuction wth the API docs to get a request working successfully; it will make it easier to put to C# (Insomnia can generate C# in two ways; it doesn't generate for my preferred library, Flurl, but it's not hard to convert)
 

Latest posts

Back
Top Bottom