该请求缺少有效的 API 键 Google 搜索 API python

The request is missing a valid API key Google search API python

我完全按照相同的步骤 here 来使用 Google 搜索 API 但我收到以下错误:

"googleapiclient.errors.HttpError: <HttpError 403 when requesting 
https://customsearch.googleapis.com/customsearch/v1?q=Persian+Cat&searchType=image&num=10&start=1&imgType=photo&fileType=jpg%7Cpng&safe=high&alt=json 
returned "The request is missing a valid API key.". Details: "The request is missing a valid API key.">" 

我认为在 google 云项目中我必须做一些事情,但我不知道,

代码(Python):

from google_images_search import GoogleImagesSearch
import os

key = os.environ.get('XXX') 
cx  = os.environ.get('XXX')
gis = GoogleImagesSearch(key, cx)
_search_params = {
    'q'       : 'Persian Cat',
    'num'     : 10,
    'safe'    : 'high',
    'fileType': 'jpg|png',
    'imgType' : 'photo',
}

gis.search(search_params=_search_params,custom_image_name='persian')
for image in gis.results():
    print('i got here')
    image.download('/images/persian') # download location
    image.resize(500, 500)            # resize the image

os.listdir('/images/persian')

还没有实际设置开发人员和项目密钥的环境变量,即在您可以key = os.environ.get()之前,您应该已经export GCS_DEVELOPER_KEY=__your_dev_api_key__在终端 window

参考documentation