无法使用Business Google V3 API获取结果

时间:2014-09-07 15:20:30

标签: python google-maps-api-3

以下示例的键已被删除/删除。

事实: 我的帐户已设置为结算。 我的帐户已启用API。 这些值与我从谷歌收到的内容相符。 当输出的URL我得到403。

以下是我的代码。关于为什么这不起作用的任何线索。我尝试使用谷歌自己的代码和youtube视频无法获胜。

>>> import sys
>>> import hashlib
>>> import urllib
>>> import hmac
>>> import base64
>>> import urlparse
>>>
>>> print("")

>>> print("URL Signer 1.0")
URL Signer 1.0
>>> print("")

>>>
>>> # Collect the URL input string (which must alrady be URL-encoded)
... # from the user and parse it into its collective components
... # For testing purposes, if no URL is entered, we use a default
... inputStr = "http://maps.googleapis.com/maps/api/geocode/json?address=New+Yor
k&client=2863356457-btgvd8ij9n51p3p8h240foam0ea4010q.apps.googleusercontent.com"

>>>
>>> print("URL To Sign: " + inputStr)
URL To Sign: http://maps.googleapis.com/maps/api/geocode/json?address=New+York&c
lient=2863356457-btgvd8ij9n51p3p8h240foam0ea4010q.apps.googleusercontent.com
>>> url = urlparse.urlparse(inputStr)
>>>
>>> privateKey = "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCA
mAwggJcAgEAAoGBAJ6MlHDfA3XFHoUi\n4gWiAOEH5Jyat8hZEGrPw7ABxzH4b1GiaVij7iDZWH09cfj
OFMF0Z8N3i3A4xlMl\n5l+gd7pI7egEuItGHVrbM8aTTVbDVWIfKh+8Cp4y9HuB+xAznPNArQs7Am1gv
hkJ\neoMij1GbcPLV7Wt0Pn87jIcxv9TpAgMBAAECgYBfCN7VRSf6yLwBi6Y1QCUMvcFv\niWHN6mzny
T9/2aD/t+1p3lMM+b373Kuo7lLJkyVnzBGCXb6r9qwSHO6YtUPt6J0q\n3wLIxRZXL7gztgjxGHCb1zC
HBCLXBQnit0cvsnTuXe+B40n5PWZoBKcQP6O2RBU8\nlDIClBP9+2tjT/eJsQJBANHrnN/W8Cyf0cvrL
bH3epMwLvs3UlxvA4wD0C0oPjqQ\nLHOncaRg+VQ32RJYP6zHo4PUmgW+JgwDBrppVj0wlG0CQQDBWi7
BczFieLENkS9n\nbyNSuAw+LR0EhEkzAbFaEEzMLXP5GI1P4pKIOpfsI7Km4mtNgGKu8e+0NvKZzRsa\
nJJztAkBh2ddNS+62jmUIDf9F5ts/QwWTWo1Mwv6/RIPEmO6w88WUv6esfPub81B/\nSZ2c1450BCUrq
iAoFz0KpdFqQnvdAkBJy5auxL1jfPbuW6xBCPztVEtv6534nFin\nvAypeDEneG2b88/zXmFY1ywmt1U
3NpGHqemAJdy75oDfTJX8e17dAkEAo/jVBhha\n9P3Vg5zrIGktnfKpw2E33jHucCNktQriCjDGDChYL
eglSMhXthYhHw3v495WLSEy\nkcR5BCwHXHA98A\u003d\u003d\n-----END PRIVATE KEY-----\n
"
>>>
>>> print("Private Key: " + privateKey)
Private Key: -----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ6MlHDfA3XFHoUi
4gWiAOEH5Jyat8hZEGrPw7ABxzH4b1GiaVij7iDZWH09cfjOFMF0Z8N3i3A4xlMl
5l+gd7pI7egEuItGHVrbM8aTTVbDVWIfKh+8Cp4y9HuB+xAznPNArQs7Am1gvhkJ
eoMij1GbcPLV7Wt0Pn87jIcxv9TpAgMBAAECgYBfCN7VRSf6yLwBi6Y1QCUMvcFv
iWHN6mznyT9/2aD/t+1p3lMM+b373Kuo7lLJkyVnzBGCXb6r9qwSHO6YtUPt6J0q
3wLIxRZXL7gztgjxGHCb1zCHBCLXBQnit0cvsnTuXe+B40n5PWZoBKcQP6O2RBU8
lDIClBP9+2tjT/eJsQJBANHrnN/W8Cyf0cvrLbH3epMwLvs3UlxvA4wD0C0oPjqQ
LHOncaRg+VQ32RJYP6zHo4PUmgW+JgwDBrppVj0wlG0CQQDBWi7BczFieLENkS9n
byNSuAw+LR0EhEkzAbFaEEzMLXP5GI1P4pKIOpfsI7Km4mtNgGKu8e+0NvKZzRsa
JJztAkBh2ddNS+62jmUIDf9F5ts/QwWTWo1Mwv6/RIPEmO6w88WUv6esfPub81B/
SZ2c1450BCUrqiAoFz0KpdFqQnvdAkBJy5auxL1jfPbuW6xBCPztVEtv6534nFin
vAypeDEneG2b88/zXmFY1ywmt1U3NpGHqemAJdy75oDfTJX8e17dAkEAo/jVBhha
9P3Vg5zrIGktnfKpw2E33jHucCNktQriCjDGDChYLeglSMhXthYhHw3v495WLSEy
kcR5BCwHXHA98A\u003d\u003d
-----END PRIVATE KEY-----

>>>
>>> #We only need to sign the path+query part of the string
... urlToSign = url.path + "?" + url.query
>>> print("")

>>> print("Original Path + Query: " + urlToSign)
Original Path + Query: /maps/api/geocode/json?address=New+York&client=2863356457
-btgvd8ij9n51p3p8h240foam0ea4010q.apps.googleusercontent.com
>>>
>>> # Decode the private key into its binary format
... # We need toe decode the URL-encoded private key
... decodedKey = base64.urlsafe_b64decode(privateKey)
>>>
>>> # Create a signature using the private key and the URL-encoded
... # string using HMAC SHA1. This signature will be binary.
... signature = hmac.new(decodedKey, urlToSign, hashlib.sha1)
>>>
>>> # Encode the binary signature into base64 for use within a URL
... encodedSignature = base64.urlsafe_b64encode(signature.digest())
>>> print("")

>>> print("B64 Signature: " + encodedSignature)
B64 Signature: bbMQO235878mM7lO0jQukCagwb8=
>>> originalUrl = url.scheme + "://" + url.netloc + url.path + "?" + url.query
>>> print("")

>>> print("Full URL: " + originalUrl + "&signature=" + encodedSignature)
Full URL: http://maps.googleapis.com/maps/api/geocode/json?address=New+York&clie
nt=2863356457-btgvd8ij9n51p3p8h240foam0ea4010q.apps.googleusercontent.com&signat
ure=bbMQO235878mM7lO0jQukCagwb8=
>>>

1 个答案:

答案 0 :(得分:0)

我最终选择了smarty streets更好的解决方案,其中包含大量很酷的地理数据以及地址。