【问题标题】:Google Cloud Datastore Emulator does not use default credentialsGoogle Cloud Datastore Emulator 不使用默认凭据
【发布时间】:2019-02-17 16:18:10
【问题描述】:

根据Google's Cloud Datastore Emulator installation instructions,我能够在bash 终端窗口中安装和运行模拟器,而gcloud beta emulators datastore start --project gramm-id 没有问题。

我还在另一个带有$(gcloud beta emulators datastore env-init) 的终端中设置了环境变量per the instructions,并验证了它们已被定义。

但是,当我运行我的 python 脚本以使用以下代码将实体添加到本地数据存储时:

from google.cloud import datastore

print(os.environ['DATASTORE_HOST'])          # output: http://localhost:8081
print(os.environ['DATASTORE_EMULATOR_HOST']) # output: localhost:8081


client = datastore.Client('gramm-id')
kind = 'Task'
name = 'simpleTask'

task_key = client.key(kind, name)
task = client.Enity(key=task_key)
task['description'] = 'Buy milk'
client.put(task)

我得到错误:

Traceback (most recent call last):
  File "tools.py", line 237, in <module>
    client = datastore.Client('gramm-id')
  File "/home/.../lib/python3.6/site-packages/google/cloud/datastore/client.py", line 205, in __init__
    project=project, credentials=credentials, _http=_http)
... long stack trace ....
  File "/home/.../lib/python3.6/site-packages/google/auth/_default.py", line 306, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.

我认为我不需要 create a GCP service account and provide access credentials 在我的机器上使用数据存储模拟器。

我的系统:

  • Ubuntu 18.04
  • Anaconda python 3.6.6
  • 谷歌云 SDK 215.0.0
  • 云数据存储模拟器 2.0.2。

我错过了什么?

【问题讨论】:

    标签: python google-cloud-platform google-cloud-datastore


    【解决方案1】:

    理论上您应该能够使用模拟凭据,例如:

    class EmulatorCreds(google.auth.credentials.Credentials):
    
        def __init__(self):
            self.token = b'secret'
            self.expiry = None
    
        @property
        def valid(self):
            return True
    
        def refresh(self, _):
            raise RuntimeError('Should never be refreshed.')
    
    client = datastore.Client(
        project='gramm-id',
        credentials=EmulatorCreds() , 
        _http=requests.Session()  # Un-authorized
    )
    

    但是it seems like this doesn't currently work,所以现在你需要设置GOOGLE_APPLICATION_CREDENTIALS

    【讨论】:

      【解决方案2】:

      gcloud auth 应用程序-默认登录

      这将提示您通过浏览器窗口登录,并为您正确设置 GOOGLE_APPLICATION_CREDENTIALS。 [1]

      【讨论】:

      • 然后我收到此错误:“用户警告:您的应用程序已使用来自 Google Cloud SDK 的最终用户凭据进行身份验证。我们建议大多数服务器应用程序改用服务帐户。如果您的应用程序继续使用最终用户凭据在 Cloud SDK 中,您可能会收到“配额超出”或“API 未启用”错误。有关服务帐户的更多信息,请参阅cloud.google.com/docs/authenticationwarnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)"
      • 我正在检查这个链接github.com/googleapis/google-cloud-python/issues/5738,它表示使用“client = datastore.Client(_http=requests.Session)”来避免请求凭据,我不确定它现在是否有效,因为我通过了身份验证通过浏览器或因为它确实有效!
      猜你喜欢
      • 2018-02-20
      • 2017-05-04
      • 2019-01-12
      • 2021-09-11
      • 2017-07-13
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2020-03-11
      相关资源
      最近更新 更多