【问题标题】:Sign google cloud storage blob using access token使用访问令牌签署谷歌云存储 blob
【发布时间】:2019-11-10 01:55:41
【问题描述】:

目标:使用 OAuth2.0 访问令牌生成签名 URL

我找到的用于签署 Google Cloud Storage Blob 的示例和源代码都需要服务帐户凭据文件(具体来说是私钥)。例如:

https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-get-object-python

但是,由于我关注the authorization flow discussed here,因此我只有 OAuth2.0 访问令牌(并且我没有有权访问 GCS 存储桶/对象的服务帐户的凭据文件和私钥)。因此,我想知道如何使用 OAuth2.0 访问令牌对 blob 进行签名。

使用的代码:

我使用以下方式对 blob 进行签名:


# First, get access token:
service_account = "<email address of a service account>"
access_token = build(
    serviceName='iamcredentials',
    version='v1',
    http=http
).projects().serviceAccounts().generateAccessToken(
    name="projects/{}/serviceAccounts/{}".format(
        "-",
        service_account),
    body=body
).execute()["accessToken"]

credentials = AccessTokenCredentials(access_token, "MyAgent/1.0", None)

# Second, use the access token to sign a blob
url = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/{}:signBlob".format(service_account)
encoded = base64.b64encode(blob)
sign_blob_request_body = {"payload": encoded}

response = requests.post(url,
                         data=json.dumps(sign_blob_request_body),
                         headers={
                             'Content-Type': 'application/json',
                             'Authorization': 'Bearer {}'.format(credentials.access_token)})
signature = response.json()["signedBlob"]

# Third, use the signature to create signed URL:
encoded_signature = base64.b64encode(signature)
signed_url = "https://storage.googleapis.com/<BUCKET>/<OBJECT>?" \
             "GoogleAccessId={}&" \
             "Expires={}&" \
             "Signature={}".format(service_account, 
                                   expiration, 
                                   encoded_signature)

收到的错误信息:

<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>
        The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
    </Message>
    <StringToSign>GET 1561832204 /<BUCKET>/<OBJECT></StringToSign>
</Error>

【问题讨论】:

  • 也可以考虑使用blob.generate_signed_url,here you have example code how to sign url
  • 如何使用访问令牌运行该方法?你有一个工作的例子吗?具体来说,如何构造带有访问令牌的客户端?
  • 使用 blob.generate_signed_url 您不需要访问令牌。要使用访问令牌而不使用 API 密钥,请检查更新的答案。

标签: google-cloud-platform google-api google-cloud-storage google-oauth


【解决方案1】:

如果您不想使用 API 密钥,follow procedure described in this sample 正在使用 iamcredentials.signBlob() API 为无需分发 API 密钥的服务帐户“远程”签名 URL。

签名字符串(必须签名)具有以下格式:

signature_string = ('{verb}\n'
                    '{content_md5}\n'
                    '{content_type}\n'
                    '{expiration}\n'
                    '{resource}')

【讨论】:

  • 如何使用调用 API 获得的签名 blob 生成签名 URL?因为当我尝试使用该 API 并生成签名 URL 时,我收到一条错误消息 SignatureDoesNotMatch
  • 注册。 GOOGLE_APPLICATION_CREDENTIALS,你在使用我链接的流程时不要使用它。
  • 请查看我更新的问题,因为您可能注意到我使用的是授权标头,因此这可能不是此问题的根源。
  • 正如我在问题开头提到的,我使用的是访问令牌,因此我没有 API 密钥。
  • 在这种情况下你需要按照这个例子:code for keyless url sign
猜你喜欢
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
  • 1970-01-01
  • 2013-06-15
  • 1970-01-01
  • 2013-01-28
  • 2014-07-25
相关资源
最近更新 更多