【问题标题】:Cannot get service account authorization to work on GCS script using Python client lib APIs无法使用 Python 客户端库 API 获得服务帐户授权以处理 GCS 脚本
【发布时间】:2012-11-21 14:25:44
【问题描述】:

在尝试编写 python 脚本以使用基于服务的授权访问 GCS 时,我想出了以下内容。请注意,“key”是我的 p12 文件的内容。

我正在尝试仅读取我帐户中的存储桶列表。我已经使用 GCS 的 Web 界面成功创建了一个存储桶,并且可以使用 gsutil 看到它。

当我执行下面的代码时,我得到一个 403 错误。起初我以为我没有得到正确的授权,但我从这个非常有用的网页(它使用基于 Web 的授权)尝试过,它可以正常工作。 https://developers.google.com/apis-explorer/#p/storage/v1beta1/storage.buckets.list?projectId=&_h=2&

当我查看标头和查询字符串并将它们与网站生成请求的 keaders 和查询进行比较时,我发现没有授权标头,并且查询字符串中没有 key= 标记。我想我认为凭证授权会为我解决这个问题。

我做错了什么?

代码:

credentials = SignedJwtAssertionCredentials(
      'xxx-my-long-email-from-the-console@developer.gserviceaccount.com',
      key,
      scope='https://www.googleapis.com/auth/devstorage.full_control')
http = httplib2.Http()
http = credentials.authorize(http)

service = build("storage", "v1beta1", http=http)

# Build the request

request = service.buckets().list(projectId="159910083329")

# Diagnostic

pprint.pprint(request.headers)
pprint.pprint(request.to_json())

# Do it!

response = request.execute()

当我尝试执行时,我得到了 403。

【问题讨论】:

  • 您在使用 AppEngine 吗?如果没有,您是否尝试过像 docs 建议那样使用 boto?
  • 感谢您修复我的格式!我采用这种方法是因为 boto 不支持服务帐户身份验证。这适用于在设备(安全摄像头)上本地运行的独立服务,它不是 AppEngine 用例。相机将其媒体上传到 GCS。

标签: authorization google-cloud-storage google-api-python-client service-accounts


【解决方案1】:

我得到了这个工作,但是,我使用的代码与您发布的 sn-p 没有根本的不同。以防万一您想将我的版本与您的版本进行比较,下面附上了对我有用的 Python 程序的完整副本。我最初得到了一个 403,就像你一样,这是由于继承了你的项目 id :)。在更新该值以使用我的项目 ID 后,我得到了正确的存储桶列表。要检查两件事:

  1. 确保您使用的项目 ID 正确,并且在 Google Developer Console 的“服务”选项卡上启用了“Google Cloud Storage JSON API”(它与其他 Google Cloud Storage API 不同)。

  2. 确保您加载的服务帐户私钥与来自开发者控制台的完全一致。我建议从下载的文件中将其读入内存,就像我在这里所做的那样,而不是尝试将其复制到代码中的字符串文字中。


#!/usr/bin/env python

import pprint
import oauth2client
from oauth2client.client import SignedJwtAssertionCredentials
import httplib2
from apiclient.discovery import build

f = open('key.p12', 'r')
key = f.read()
f.close()

credentials = SignedJwtAssertionCredentials(
      'REDACTED',
      key,
      scope='https://www.googleapis.com/auth/devstorage.full_control')
http = httplib2.Http()
http = credentials.authorize(http)

service = build("storage", "v1beta1", http=http)

# Build the request

request = service.buckets().list(projectId="REDACTED")

# Diagnostic

pprint.pprint(request.headers)
pprint.pprint(request.to_json())

# Do it!

response = request.execute()
pprint.pprint(response)

【讨论】:

  • 嗨,马克,感谢您的积极回应。我最初认为我没有启用 GCS JSON API 是问题所在,但后来我能够从我上面提到的网页中获得一个 OK 和一个桶列表。也许该页面使用了不同的身份验证方案...我昨天请求访问 JSON API,但尚未被授予。获得批准后我会再试一次。
  • 如果您所说的网页是指 Google Cloud Storage Web UI,它使用旧的 XML API,而不是新的 JSON API,因此不一定会通过 JSON API 验证您的访问。鉴于您昨天请求访问,我敢打赌那是您的问题。无论如何,如果您没有尽快获得访问权限或获得访问权限但它不起作用,请在此处发表评论。
  • 谢谢 Marc - 刚刚获得批准,它按预期工作。我被抛出是因为基于 Web 的 api-explorer (developers.google.com/apis-explorer/#p/storage/v1beta1/…) 返回 JSON,所以我推测它使用的是 JSON API。
  • 很高兴听到这个消息,大卫。欢迎!
猜你喜欢
  • 2016-06-21
  • 1970-01-01
  • 2017-02-22
  • 2018-09-03
  • 2018-07-07
  • 2018-10-02
  • 2017-02-11
  • 2015-05-14
  • 1970-01-01
相关资源
最近更新 更多