我基本上做了以下事情: https://developers.google.com/maps/documentation/business/webservices/auth
在我的 MacBook 上的 Python 2.7.3 和 Windows 64 位服务器环境上的 2.7.5 中,我无法重现正确的签名,而我完全按照原始示例进行操作。我做了一个这样的函数:
import sys
import hashlib
import urllib
import hmac
import base64
import urlparse
def process_url(input_url, private_key):
print("URL To Sign: " + input_url)
url = urlparse.urlparse(input_url)
print("Private Key: " + private_key)
url_to_sign = url.path + "?" + url.query
print("Original Path + Query: " + url_to_sign)
decoded_key = base64.urlsafe_b64decode(private_key)
signature = hmac.new(decoded_key, url_to_sign, hashlib.sha1)
encodedSignature = base64.urlsafe_b64encode(signature.digest())
print("B64 Signature: " + encodedSignature)
original_url = url.scheme + "://" + url.netloc + url.path + "?" + url.query
full_url = original_url + "&signature=" + encodedSignature
print "Full URL: " + full_url
return full_url
现在根据谷歌的说法应该给出以下内容:
- 网址:http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client= {客户端ID}
- 私钥:vNIXE0xscrmjlyV-12Nj_BvUPaw=
- 要签名的网址部分:/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
- 签名:KrU1TzVQM7Ur0i8i7K3huiw3MsA=
- 完整签名网址:http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client= {clientID}&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA=
但是,当我执行以下操作时:
process_url('http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}', 'vNIXE0xscrmjlyV-12Nj_BvUPaw=')
我得到:
- 签名网址:http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client= {客户端ID}
- 私钥:vNIXE0xscrmjlyV-12Nj_BvUPaw=
- 原始路径 + 查询:/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
- B64 签名:WlcBIkr9WMB9uPhXWmAGcjG_2M4=
- 完整网址:http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client= {clientID}&signature=WlcBIkr9WMB9uPhXWmAGcjG_2M4=
所以我得到“WlcBIkr9WMB9uPhXWmAGcjG_2M4=”而不是“KrU1TzVQM7Ur0i8i7K3huiw3MsA=”。我发誓这曾经有效,但我在不同的系统上始终得到这个新的 W 值。
有人知道我做错了什么吗?页面不正确还是我做的事情根本不正确??
最佳答案
您没有做错任何事情,您正在签署不同的数据,从而获得不同的签名。
在 Google 示例中,如果您复制并粘贴,您会发现剪贴板中省略了大括号;所以它签署了以下 URL:
http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID
但是当您尝试时,您会逐字复制它,包括大括号:
http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client={clientID}
所以庆幸一下,你的代码是正确的,唯一的问题出在示例数据中!
关于python - Python 中的 Google Map API 签名生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19729430/