我正在使用django,需要从Google街景中获取图片。 我已经从google文档中阅读了几乎所有有关它的内容,但是即使是他们的python代码示例也无法为我工作。
我知道google并没有提供图片的静态链接,但是即使生成了链接(带有API令牌),我也无法访问文件,为什么?
当我从他们的文档的浏览器中生成该代码时,对我来说已经工作了好几次,但似乎只能访问一次。
有我的代码,不过没什么特别的:
import hashlib
import hmac
import base64
import imghdr
from urllib.parse import urlparse
import requests
def sign_url(input_url=None, secret=None):
if not input_url or not secret:
raise Exception("Both input_url and secret are required")
url = urlparse(input_url)
# We only need to sign the path+query part of the string
url_to_sign = url.path + "?" + query
# Decode the private key into its binary format
# We need to decode the URL-encoded private key
decoded_key = base64.urlsafe_b64decode(secret)
# Create a signature using the private key and the URL-encoded
# string using HMAC SHA1. This signature will be binary.
signature = hmac.new(decoded_key, url_to_sign.encode('utf-8'), hashlib.sha1)
# Encode the binary signature into base64 for use within a URL
encoded_signature = base64.urlsafe_b64encode(signature.digest())
original_url = url.scheme + "://" + url.netloc + url.path + "?" + query
# Return signed URL
return original_url + "&signature=" + encoded_signature.decode('utf-8')
if __name__ == "__main__":
input_url = “” # URL, you can find one from google docs because I cannot provide my api token
secret = “” # SAME WITH SECRET TOKEN
url = sign_url(input_url, secret)
req = requests.post(url=url)
photo_type = imghdr.what("", req.content)
query = "file"
photo_name = (query + "." + photo_type).replace(" ", "_")
with open(photo_name, "wb") as photo:
photo.write(req.content)
它只是生成url并尝试下载文件
如果您不知道我在说什么,请链接到Google文档 https://developers.google.com/maps/documentation/streetview/intro