Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何检查Google street view API是否没有返回图像或API密钥是否过期?_Python_Api_Google Maps_Google Maps Api 3_Google Street View - Fatal编程技术网

Python:如何检查Google street view API是否没有返回图像或API密钥是否过期?

Python:如何检查Google street view API是否没有返回图像或API密钥是否过期?,python,api,google-maps,google-maps-api-3,google-street-view,Python,Api,Google Maps,Google Maps Api 3,Google Street View,我想用python下载一些图像 有时一个区域没有图像返回,但是可以使用API键 其他时间API密钥过期或无效,也无法返回图像 The Google Maps API server rejected your request. The provided API key is expired. 如何用Python中的代码区分这两种情况 非常感谢。一种方法是使用请求库进行api调用,并解析JSON响应: import requests url = 'https://maps.googleapis.

我想用python下载一些图像

有时一个区域没有图像返回,但是可以使用API键

其他时间API密钥过期无效,也无法返回图像

The Google Maps API server rejected your request. The provided API key is expired.
如何用Python中的代码区分这两种情况


非常感谢。

一种方法是使用
请求库进行api调用,并解析JSON响应:

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')
现在
error\u message
将是错误消息的文本(例如,
“提供的API密钥已过期”。
),或者如果没有错误消息,则将是
None
。因此,在以后的代码中,您可以检查是否有错误消息,并根据消息的内容执行操作:

if error_message and 'The provided API key is invalid.' in error_message:
    do_something()
elif ...:
    do_something_else()
如果您只想查看请求是否成功,还可以检查键
“状态”

status = results.get('status')

一种方法是使用
请求
库进行api调用,并解析JSON响应:

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')
现在
error\u message
将是错误消息的文本(例如,
“提供的API密钥已过期”。
),或者如果没有错误消息,则将是
None
。因此,在以后的代码中,您可以检查是否有错误消息,并根据消息的内容执行操作:

if error_message and 'The provided API key is invalid.' in error_message:
    do_something()
elif ...:
    do_something_else()
如果您只想查看请求是否成功,还可以检查键
“状态”

status = results.get('status')

如何检查区域中是否有图像?@karl\u TUM我假设
results.get('status')
将返回字符串
'ZERO\u results'
,但您必须进行测试。如果这不起作用,请查看文档,如果您仍然无法理解,请在此网站上提出新问题。祝你好运不,什么不行。查看配方,以确定给定位置是否有街景全景。如何检查该区域是否有图像?@karl\u TUM我假设在这种情况下,
results.get('status')
将返回字符串
'ZERO\u results'
,但您必须进行测试。如果这不起作用,请查看文档,如果您仍然无法理解,请在此网站上提出新问题。祝你好运不,什么不行。有关查找给定位置是否有街景全景的方法,请参见。