HTTP错误400错误请求(其他类似的问题并不能帮助我)

时间:2016-10-12 00:00:55

标签: python google-api google-api-python-client

其他类似名称的问题都没有解决我的问题。

为什么我收到此错误?我从google' s 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">

1 个答案:

答案 0 :(得分:1)

我尝试访问bad URL,并说原因是&#34; keyExpired&#34;。您在此处输入的代码还包括以下文档:

  

访问Google API控制台http://code.google.com/apis/console   获取自己应用程序的API密钥。

尝试按照说法行事。祝你好运!