0

I want to use Google Street View API to download some images in python.

Sometimes there is no image return in one area, but the API key can be used.

enter image description here

Other time API key is expired or invalid, and also cannot return image.

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

How to distinguish these two situations with code in Python?

Thank you very much.

karl_TUM
  • 5,769
  • 10
  • 24
  • 41

1 Answers1

2

One way to do this would be to make an api call with the requests library, 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 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. So later in your code you can check if there is an error message, and do things based on the content of the message:

if error_message and 'The provided API key is invalid.' in error_message:
    do_something()
elif ...:
    do_something_else()

You could also check the key 'status' if you just want to see if the request was successful or not:

status = results.get('status')
elethan
  • 16,408
  • 8
  • 64
  • 87
  • how to check whether there is image in the region? – karl_TUM Sep 26 '16 at 08:53
  • @karl_TUM I assume it that case that `results.get('status')` will return the string `'ZERO_RESULTS'`, but you would have to test. If that doesn't work, check the documentation, and if you still can't figure it out, ask a new question on this site. Good luck! – elethan Sep 26 '16 at 12:34
  • Nope, what won't work. See http://stackoverflow.com/a/40803070/1069142 for the recipe to find whether there is a Street View panorama at given locations. – miguev Nov 25 '16 at 11:22