Skip to main content
 首页 » 编程设计

python之HTTP 错误 400 错误请求(其他类似问题对我没有帮助)

2025年04月02日8cyq1162

其他同名问题都没有解决我的问题。

为什么会出现此错误?我从谷歌的 github 复制并粘贴了这个示例代码。

import pprint 
 
from googleapiclient.discovery import build 
 
def main(): 
  # Build a service object for interacting with the API. Visit 
  # the Google APIs Console <http://code.google.com/apis/console> 
  # to get an API key for your own application. 
  service = build("customsearch", "v1", 
        developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") 
 
  res = service.cse().list( 
      q='lectures', 
      cx='017576662512468239146:omuauf_lfve', 
    ).execute() 
  pprint.pprint(res) 
 
if __name__ == '__main__': 
    main() 

Here is where I go the code from

我收到这个错误:

Traceback (most recent call last): 
  File "testpython", line 17, in <module> 
    main() 
  File "testpython", line 12, in main 
    cx='017576662512468239146:omuauf_lfve', 
  File "/home/mddrill/.local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper 
    return wrapped(*args, **kwargs) 
  File "/home/mddrill/.local/lib/python2.7/site-packages/googleapiclient/http.py", line 838, in execute 
    raise HttpError(resp, content, uri=self.uri) 
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/customsearch/v1?q=lectures&alt=json&cx=017576662512468239146%3Aomuauf_lfve&key=AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0 returned "Bad Request"> 

请您参考如下方法:

我试图访问那个 bad URL并且说原因是“keyExpired”。您放在这里的代码还包括以下文档:

Visit the Google APIs Console http://code.google.com/apis/console to get an API key for your own application.

试着按照它说的去做。祝你好运!