Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am using the Google Street View API for downloading some images in Python. What happening is sometimes there is no image return in one area, but the API key can be used.

enter image description here

Some other time, I am getting the API key is expired or invalid, and also cannot return the image.

The Google Maps API server denied your request. The provided API key is expired.

How to identify these two situations with code in Python?

1 Answer

0 votes
by (107k points)

What you can do is simply generate an API call with the requests package, and parse the JSON response:

import requests

URL = 'https://maps.googleapis.com/maps/api/streetview?size=600x300&location=46.414382,10.013988&heading=151.78&pitch=-0.76&key=YOUR_API_KEY'

r = requests.get(url)

results = r.json()

error_message = results.get('error_message')

Now error_message variable will be the text of the error message (e.g., 'The provided API key is expired.'), or will be None if there is no error message. 

if error_message and 'The provided API key is invalid.' in error_message:

    do_something()

elif ...:

    do_something_else()

It will be better if you also check the key 'status' so that you can see if the request was successful or not:

status = results.get('status')

Kick-start your career in Python with the perfect below Python tutorial video for better understanding:

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 10, 2019 in Python by Sammy (47.6k points)
0 votes
0 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...