【问题标题】:Get "keyInvalid" when requesting Google Custom Search API请求 Google 自定义搜索 API 时获取“keyInvalid”
【发布时间】:2017-10-20 06:19:40
【问题描述】:

我正在尝试自动搜索关于Programmatically searching google in Python using custom search 的查询,并在简单搜索时不断收到此错误:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "keyInvalid",
    "message": "Bad Request"
   }
  ],
  "code": 400,
  "message": "Bad Request"
 }
}

这是我的代码:

import os
import pprint
from googleapiclient.discovery import build

api_key = os.environ.get('API_KEY', None)
cse_id = os.environ.get('CSE_ID', None)


def google_search(search_term, api_key, cse_id, **kwargs):
    service = build("customsearch", "v1", developerKey=api_key)
    res = service.cse().list(q=search_term, cx=cse_id, **kwargs).execute()
    return res['items']


results = google_search(
    'coding', api_key, cse_id, num=3)
for result in results:
    pprint.pprint(result)

我仔细检查了 API 密钥,绝对没有超出使用限制(只运行了几次程序)。最初配置 API 密钥时,我确实将 API 密钥限制为我的 IP 地址(不确定这是否是潜在原因;我是 API 新手,只想安全地测试它)。

【问题讨论】:

    标签: python python-3.x google-custom-search google-search-api


    【解决方案1】:

    啊..想通了。我将 API 密钥存储为带有“”的环境变量,这以某种方式导致它在值中包含引号,从而使其无效。现在可以使用了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多