【问题标题】:How to initialize google-cloud-firestore - Python如何初始化 google-cloud-firestore - Python
【发布时间】:2021-08-03 15:44:18
【问题描述】:
from google.cloud import firestore

# Add a new document
db = firestore.Client()  # need to put credentials
doc_ref = db.collection(u'users').document(u'alovelace')
doc_ref.set({
    u'first': u'Ada',
    u'last': u'Lovelace',
    u'born': 1815
})

# Then query for documents
users_ref = db.collection(u'users')

for doc in users_ref.stream():
    print(u'{} => {}'.format(doc.id, doc.to_dict()))

这是来自https://googleapis.dev/python/firestore/latest/ 的示例代码 当我运行时它显示

Traceback (most recent call last):
  File "/home/kulothungan/Purp/temp/temp.py", line 4, in <module>
    db = firestore.Client()
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/client.py", line 94, in __init__
    client_options=client_options,
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/base_client.py", line 128, in __init__
    _http=None,
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 277, in __init__
    _ClientProjectMixin.__init__(self, project=project, credentials=credentials)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 225, in __init__
    project = self._determine_default(project)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 244, in _determine_default
    return _determine_default_project(project)
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/_helpers.py", line 186, in _determine_default_project
    _, project = google.auth.default()
  File "/home/kulothungan/.local/lib/python3.6/site-packages/google/auth/_default.py", line 483, 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://cloud.google.com/docs/authentication/getting-started

但我不知道如何初始化它,而且我不想通过使用环境变量来做到这一点 如果有人知道请告诉我 提前致谢

【问题讨论】:

    标签: python firebase google-cloud-firestore


    【解决方案1】:

    如果您有权访问此特定项目,则需要转到 Google 云控制台服务帐户页面 here 并创建一个服务帐户以访问 firestore。 Service account creation。根据需要为服务帐户提供数据存储区roles 之一。

    创建后,您必须为服务帐户生成密钥。您可以从服务帐户页面上的“密钥”标签执行此操作。

    选择 JSON 格式。您将获得一个名为keys.json 的文件。将其存储在您的计算机上并创建环境变量GOOGLE_APPLICATION_CREDENTIALS

    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keys.json
    

    现在尝试运行你的 python 脚本。

    不使用环境变量

    您需要安装google-auth python 包,该包提供google.oauth2.service_account 模块,其中有Credentials 类。您可以使用您的凭据初始化此类,如下所示:

    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file(
        '/path/to/keys.json')
    

    然后你可以像这样将这个 credentials 对象传递给 firestore Client()

    db = firestore.Client(credentials=credentials)
    

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 2019-03-19
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2021-04-17
      • 2021-05-17
      • 2020-08-26
      相关资源
      最近更新 更多