为什么我从 python 调用的 google 自定义搜索 API 不起作用?

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

提问人:anonymous developer 提问时间:6/25/2023 最后编辑:anonymous developer 更新时间:6/27/2023 访问量:130

问:

当我运行以下代码时,我收到错误响应(请参阅第二个代码段)。我已经检查并确保GOOGLE_SEARCH_KEY和SEARCH_ENGINE_ID有效,并且我已正确设置账单。我已从错误消息中删除敏感数据并放入占位符中。可能是什么问题?提前致谢。

            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)

文件“x.py”,第 34 行,在搜索中 ).execute() ^^^^^^^^^ 文件 “/usr/local/lib/python3.11/site-packages/googleapiclient/_helpers.py”, 第 130 行,在 positional_wrapper 返回中换行(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^文件 “/usr/local/lib/python3.11/site-packages/googleapiclient/http.py”, 第 938 行,在执行中引发 HttpError(resp, content, uri=self.uri) <请求 https://customsearch.googleapis.com/customsearch/v1?q=%22Software%22&cx=<SEARCH_ENGINE_ID>&num=100&key=<API_KEY>&alt=json 时出现 HttpError 400 返回“请求包含无效参数”。详: “[{'message': '请求包含无效参数。', 'domain': 'global', 'reason': 'badRequest'}]“>

google-api-python-client google-custom-search google-search-api 错误请求

评论

2赞 John Gordon 6/25/2023
不要让我们猜测错误在哪里。请编辑问题并添加完整的错误回溯消息。
0赞 anonymous developer 6/25/2023
这是我从 API 调用中得到的完整错误,它没有给我任何其他数据。我已经尝试了整个事情,除了块和例外是我粘贴在第二个代码段上的那个。
1赞 John Gordon 6/25/2023
我已经将整个事情包装在一个尝试和除了块中那么你没有向我们展示你的真实代码,因为发布的代码没有这样的 try/except 块。
1赞 John Gordon 6/25/2023
当您不显示真实代码时,很难提供帮助。
0赞 anonymous developer 6/25/2023
好的,我编辑了帖子以包含 try except 块。

答: 暂无答案