程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

来自GAE的Cloud Storage API请求-403访问权限未配置

发布于2020-01-17 18:08     阅读(365)     评论(0)     点赞(30)     收藏(0)


我的GAE应用正在尝试处理存储在Google云端存储中的文件。

这些文件存储在我的应用程序的默认存储区中。我已经使用GCS Python客户端库(https://developers.google.com/appengine/docs/python/googlecloudstorageclient/设法将文件读取/写入该存储桶

不幸的是,它不支持复制。相反,我正在尝试通过API客户端库(https://google-api-client-libraries.appspot.com/documentation/storage/v1/python/latest/storage_v1.objects.html)和服务帐户使用JSON API https://developers.google.com/api-client-library/python/guide/google_app_engine#ServiceAccounts

到目前为止,请求云存储URL时出现错误403。

这是代码:

credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/devstorage.read_write')
http = credentials.authorize(httplib2.Http(memcache))
service = discovery.build('storage', 'v1', http=http, developerKey='api_key_generated_from_the_dev_console')
bucket_name = app_identity.get_default_gcs_bucket_name()

# I'm planning to batch multiple requests, although there is just one in this example
batch = BatchHttpRequest()

# process_list_response outputs the exception if any
batch.add(service.objects().list(bucket=bucket_name), callback=process_list_response) 
batch.execute(http=http)

这是日志:

要求的网址:https : //www.googleapis.com/discovery/v1/apis/storage/v1/rest?userIp=xxxx

尝试刷新以获得初始access_token

要求的网址:https : //www.googleapis.com/storage/v1/b/xxx.appspot.com/o?alt=json

请求https://www.googleapis.com/storage/v1/b/xxx-dev.appspot.com/o?alt=json时 返回HttpError 403 未配置访问权限。请使用Google Developers Console激活您的API项目。

这是我在开发控制台中所做的事情:

  • Google Cloud Storage和Google Cloud Storage JSON API已切换为ON。
  • 我创建了一个用于构建服务的API密钥(因为我也使用Oauth,所以这是必要的吗?)
  • 在权限下,我通过电子邮件xxx@appspot.gserviceaccount.com为我的应用添加了一个成员

我该如何进行这项工作?


解决方案


似乎是我的编辑(我们一起工作)被默默地拒绝了,因此将其发布为答案,并且评论太有限。这不是答案,但这正在扩展问题。

单个http请求的简单示例。似乎JSON API根本无法在API资源管理器外部运行。XML / REST API起作用并返回存储桶中的文件列表。

credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/devstorage.read_write')
http = credentials.authorize(httplib2.Http(memcache))

bucket_name = app_identity.get_default_gcs_bucket_name()

# This works (200 with list of files in the content)
request_url = 'http://commondatastorage.googleapis.com/' + bucket_name
response, content = http.request(request_url, method="GET")

# This doesn't work (403, Access not configured)
request_url = 'https://www.googleapis.com/storage/v1/b/' + bucket_name + '/o?alt=json'
response, content = http.request(request_url, method="GET")

# This doesn't work (403, Access not configured), the key and project id header seem useless.
request_url = 'https://www.googleapis.com/storage/v1/b/' + bucket_name + '/o?alt=json&key=' + API_KEY
response, content = http.request(request_url, method="GET", headers={'x-goog-project-id': PROJECT_ID})

另外,查看AppAssertionCredentials的代码,我们可以看到:

  kwargs: optional keyword args, including:
    service_account_id: service account id of the application. If None or
      unspecified, the default service account for the app is used.

self.service_account_id = kwargs.get('service_account_id', None)

将任何内容作为service_account_id参数传递都会导致异常:

Traceback (most recent call last):
  File "/base/data/home/apps/.../1.37.../backup.py", line 61, in get
    response, content = http.request(request_url, method="GET")
  File "/base/data/home/apps/.../1.377.../oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/base/data/home/apps/.../1.37.../oauth2client/client.py", line 491, in new_request
    self._refresh(request_orig)
  File "/base/data/home/apps/.../1.37.../oauth2client/appengine.py", line 197, in _refresh
    raise AccessTokenRefreshError(str(e))
AccessTokenRefreshError

我已经测试过传递传回的值app_identity.get_service_account_name(),但这是行不通的。(即使文档说它会使用“该应用程序的默认服务帐户”,如果未设置)。

我已经测试过通过在开发人员控制台中找到的格式为的服务帐户电子邮件3....-v0....@developer.gserviceaccount.com相同的令牌例外。

那么,当在api / services下明确启用Cloudstorage JSON API时,为什么为什么未配置403 Access?

为什么将service_account_id传递给AppAssertionCredentials失败,并出现AccessTokenRefreshError?

编辑:

解决方案太荒谬了:关闭Google Cloud Storage API,然后再重新打开。

我认为该应用程序是“旧版”应用程序,因此在这里使最后一个要点12起作用:https : //developers.google.com/appengine/docs/python/googlecloudstorageclient/activate



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/228359/fc1698c2ad75d5c4f50f/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

30 0
收藏该文
已收藏

评论内容:(最多支持255个字符)