【问题标题】:Invalid credentials - 401 when trying to access Google Datastore from a GCE instance无效凭据 - 尝试从 GCE 实例访问 Google 数据存储时出现 401
【发布时间】:2016-08-20 01:23:13
【问题描述】:

我正在尝试在此 URL 上找到的 adams.py 示例 https://cloud.google.com/datastore/docs/getstarted/start_python/

正如这个网址中所解释的那样:

如果您在 Google Compute Engine 上运行此代码,则不必创建新的服务帐户凭据,因为在创建具有数据存储和 userinfo-email 范围的实例时会使用项目的默认服务帐户。

在创建我的实例时,我检查了选项:

允许 API 访问同一项目中的所有 Google Cloud 服务。

当我跑步时

python adams.py 我的实例

我明白了:

    ERROR:root:Error while doing datastore operation
    ERROR:root:RPCError: beginTransaction Invalid Credentials
    ERROR:root:HTTPError: 401 Unauthorized

事实上,即使我为实例使用了错误的名称,我也会收到相同的消息。

我的问题似乎与一年前的这个帖子相似:Connect google datastore from existing google compute engine in python

在我自己的代码中,它可以正常访问 BigQuery,然后当涉及到 Datastore 时,它​​会引发相同的错误,但至少更具体地解决了最后一行:resp = datastore.commit(req) .下面是使用Datastore的部分代码。

req = datastore.CommitRequest()
req.mode = datastore.CommitRequest.NON_TRANSACTIONAL
dsDashboard = req.mutation.insert_auto_id.add()

path_element = dsDashboard.key.path_element.add()
path_element.kind = 'Dashboard'
fmt = '%Y-%m-%d'
dashboardDate = dsDashboard.property.add()
dashboardDate.name = 'Date'
dashboardDate.value.string_value = dtDay.strftime(fmt)

dashboardValue = dsDashboard.property.add()
dashboardValue.name = 'Value'
dashboardValue.value.indexed = False
dashboardValue.value.string_value = encode(jDashboard)

resp = datastore.commit(req)

我的代码的错误:

    Traceback (most recent call last):
      File "code.py", line 262, in <module>
        main()
      File "code.py", line 256, in main
        resp = datastore.commit(req)
      File "/usr/local/lib/python2.7/dist-packages/googledatastore/__init__.py", line 90, in commit
        return get_default_connection().commit(request)
      File "/usr/local/lib/python2.7/dist-packages/googledatastore/connection.py", line 135, in commit
        datastore_v1_pb2.CommitResponse)
      File "/usr/local/lib/python2.7/dist-packages/googledatastore/connection.py", line 195, in _call_method
        raise RPCError(method, response, content)
    googledatastore.connection.RPCError: commit RPC client failure with HTTP(401) Unauthorized: Invalid Credentials    

简而言之,这似乎很简单。这里有人遇到过同样的问题并找到了解决方法吗?

【问题讨论】:

  • 你检查过提供的答案吗?
  • 嗨。谢谢!在您回答之前,我不得不使用一种解决方法。我对你的解决方案很好奇,但我没有时间测试。还是谢谢!

标签: python google-compute-engine google-cloud-datastore protocol-buffers


【解决方案1】:

这里的问题是:

在创建我的实例时,我检查了选项:

允许 API 访问同一项目中的所有 Google Cloud 服务。

有人会假设这会使您的实例获得所有范围,但它似乎不是这样工作的。我试过了,如果你检查一个新创建的实例:

gcloud compute instances describe my-instance

你会发现它只有一个作用域

scopes:
  - https://www.googleapis.com/auth/cloud-platform

这里的解决方案是

  • 通过开发者控制台创建实例时,手动启用“用户信息”和“云数据存储”(“管理、磁盘、网络、访问和安全选项”>“访问和安全”)
  • 确保在使用 gcloud 命令时使用正确的范围标志(--scopes datastore,userinfo-email),如Getting started with Google Cloud Datastore and Python/Protobuf 文章中所述

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多