Home > database >  The request is missing a valid API key Google search API python
The request is missing a valid API key Google search API python

Time:04-27

I followed the same steps here exactly to use the Google search API but I got the following error:

"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|png&safe=high&alt=json 
returned "The request is missing a valid API key.". Details: "The request is missing a valid API key.">" 

I think there is something that I have to do in google cloud project but I can't know,

Code (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')

CodePudding user response:

You haven't actually set the environment variable for the Developer and project keys i.e. before you can do key = os.environ.get(), you should have done export GCS_DEVELOPER_KEY=__your_dev_api_key__ in a terminal window

Refer to the documentation

  • Related