anonymous developer
anonymous developer

Reputation: 406

Why is my google custom search API call from python not working?

When I run the following code I am getting an error response (see the second snippet). I have checked and made sure the GOOGLE_SEARCH_KEY and the SEARCH_ENGINE_ID are valid and I have billing set up properly. I have removed sensitive data from the error message and put in placeholders. What could be the issue? Thanks in advance.

            from googleapiclient.discovery import build

            try:

                api_key = GOOGLE_SEARCH_KEY
                search_engine_id = SEARCH_ENGINE_ID
                query = "test"
                num_results = 100

                service = build("customsearch", "v1", 
                         developerKey=api_key)

                response = service.cse().list(
                    q=query,
                    cx=search_engine_id,
                    num=num_results
                ).execute()
            except Exception as e:
                print(e)

File "x.py", line 34, in search ).execute() ^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper return wrapped(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/googleapiclient/http.py", line 938, in execute raise HttpError(resp, content, uri=self.uri) <HttpError 400 when requesting https://customsearch.googleapis.com/customsearch/v1?q=%22Software%22&cx=<SEARCH_ENGINE_ID>&num=100&key=<API_KEY>&alt=json returned "Request contains an invalid argument.". Details: "[{'message': 'Request contains an invalid argument.', 'domain': 'global', 'reason': 'badRequest'}]">

Upvotes: -1

Views: 682

Answers (1)

Take
Take

Reputation: 1

Change num_results = 100 to num_results = 10.

Upvotes: 0

Related Questions