首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何签署来自python库的谷歌街景请求?

如何签署来自python库的谷歌街景请求?
EN

Stack Overflow用户
提问于 2019-07-02 12:58:06
回答 2查看 652关注 0票数 1

我正在尝试对google地图静态街景api进行大量的查询。我正在提供我的Api密钥,它工作得很好,但似乎当它达到一定数量的抓取完成时,我开始收到google返回的禁止错误。经过一些探索之后,我确定这是因为我没有用我的签名秘密来签署这些请求。

在探索了google云控制台之后,我发现了我的签名秘密,它看起来像这样:

代码语言:javascript
复制
'sadfafKL2-pw43434sPSds2DDk=' #(this is a random string, not actual signing secret)

我现在的困惑集中在如何使用导入google_streetview.api包将这个签名秘密传递给我的google maps请求参数。我之前使用的获取apikey而不是签名密钥的当前代码是:

代码语言:javascript
复制
import google_streetview.api
params = [{
    'size': '640x640',  
    'location':  "34.122342,-118.73721", 
    'key': 'my_key'
}]
results = google_streetview.api.results(params)

我如何才能将我的签名秘密添加到其中以停止禁止的错误?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-09 18:33:01

请按照谷歌文档中的说明创建数字签名https://developers.google.com/maps/documentation/streetview/get-api-key#dig-sig-key

您可以在Python here中找到示例代码。

希望这能有所帮助!

票数 0
EN

Stack Overflow用户

发布于 2020-05-14 01:39:27

Google在文档中提供的代码示例已过时(Python2.x?)。他们链接到的GitHub中的文件自2016年以来就没有更新过!我提交了一份PR和Python3的更新文件,但是repo已经超过2年没有更新过了,所以谁知道他们会不会看呢。下面是代码,以防万一:

代码语言:javascript
复制
import hashlib
import hmac
import base64
import urllib.parse as urlparse


def sign_url(input_url=None, secret=None):
    """ Sign a request URL with a URL signing secret.
      Usage:
      from urlsigner import sign_url
      signed_url = sign_url(input_url=my_url, secret=SECRET)
      Args:
      input_url - The URL to sign
      secret    - Your URL signing secret
      Returns:
      The signed request URL
  """

    if not input_url or not secret:
        raise Exception("Both input_url and secret are required")

    url = urlparse.urlparse(input_url)

    # We only need to sign the path+query part of the string
    url_to_sign = url.path + "?" + url.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, str.encode(url_to_sign), 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 + "?" + url.query

    # Return signed URL
    return original_url + "&signature=" + encoded_signature.decode()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56845587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档