最近,我开始使用地址来解决Google地图地理编码服务的问题。
当发送LatLng请求(如下)时,请求成功完成,提供所需的JSON,但是在发送地址时(使用相同的签名代码并签名继续),我收到403
,并显示错误消息:
无法验证请求。
提供的“签名”对提供的客户端ID无效。了解详情:https://developers.google.com/maps/documentation/business/webservices/auth
LatLng
- https://maps.googleapis.com/maps/api/geocode/json?latlng=37.092590332%2C-8.78356933594&client=revoked&sensor=true&signature=revoked Address
- https://maps.googleapis.com/maps/api/geocode/json?signature=revoked&client=revoked&sensor=true&address=SW1A+2 我玩过http
vs https
,不同类型的网址编码等,但似乎无法让它发挥作用。
有人有什么想法吗?
为了完整起见,我在下面提供了我的签名代码:
class GoogleGeocoder(Geocoder):
url = 'https://maps.googleapis.com/maps/api/geocode/json'
def build_params(self):
if self.lon and self.lat:
self.get_params = {
'latlng': '{lat},{lon}'.format(
lat=self.lat,
lon=self.lon
)
}
elif self.address:
self.get_params = {
'address': self.address
}
else:
raise Exception("Must provide either an address or (lon, lat)")
self.get_params['sensor'] = 'true'
self.get_params['client'] = GOOGLE_CLIENT_STRING
req = requests.Request('GET', self.url, params=self.get_params)
r = req.prepare()
url_for_signing = r.path_url
decodedKey = base64.urlsafe_b64decode(GOOGLE_SIGNING_KEY)
signature = hmac.new(decodedKey, url_for_signing, hashlib.sha1)
encodedSignature = base64.urlsafe_b64encode(signature.digest())
self.get_params['signature'] = encodedSignature
return self