对本地 firestore 模拟器进行身份验证时出现 DefaultCredentialsError
DefaultCredentialsError while authenticating to local firestore emulator
我在将 AnonymousCredentials 与 firestore 模拟器结合使用时遇到问题。使用 service-account.json 访问 firestore 工作正常。我的目标是删除 service-account.json 并改用 AnonymousCredentials。 url 和端口配置为环境变量“FIRESTORE_EMULATOR_HOST”。凭据设置如下:
from google.auth.credentials import AnonymousCredentials
from google.cloud import firestore
def main():
db = firestore.Client(credentials=AnonymousCredentials())
result = db.collection('some-collection').document('some-document').get()
print(result.to_dict())
但是,代码引发了 DefaultCredentialsError:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/src/app/__main__.py", line 12, in <module>
main()
File "/usr/src/app/__main__.py", line 6, in main
db = firestore.Client(credentials=AnonymousCredentials())
File "/usr/local/lib/python3.10/site-packages/google/cloud/firestore_v1/client.py", line 94, in __init__
super(Client, self).__init__(
File "/usr/local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_client.py", line 125, in __init__
super(BaseClient, self).__init__(
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 318, in __init__
_ClientProjectMixin.__init__(self, project=project, credentials=credentials)
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 266, in __init__
project = self._determine_default(project)
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 285, in _determine_default
return _determine_default_project(project)
File "/usr/local/lib/python3.10/site-packages/google/cloud/_helpers/__init__.py", line 154, in _determine_default_project
_, project = google.auth.default()
File "/usr/local/lib/python3.10/site-packages/google/auth/_default.py", line 575, 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 h
ttps://cloud.google.com/docs/authentication/getting-started
我在 GCP 支持的帮助下解决了这个问题:
如果设置 FIRESTORE_EMULATOR_HOST,则需要说明中提到的凭据。您还需要通过环境变量 GOOGLE_CLOUD_PROJECT 设置项目 ID。该值应以“demo-”开头。例如。您可以将 GOOGLE_CLOUD_PROJECT 设置为“demo-firestore”。设置这个环境变量解决了这个问题。
我在将 AnonymousCredentials 与 firestore 模拟器结合使用时遇到问题。使用 service-account.json 访问 firestore 工作正常。我的目标是删除 service-account.json 并改用 AnonymousCredentials。 url 和端口配置为环境变量“FIRESTORE_EMULATOR_HOST”。凭据设置如下:
from google.auth.credentials import AnonymousCredentials
from google.cloud import firestore
def main():
db = firestore.Client(credentials=AnonymousCredentials())
result = db.collection('some-collection').document('some-document').get()
print(result.to_dict())
但是,代码引发了 DefaultCredentialsError:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/local/lib/python3.10/runpy.py", line 86, in _run_code
exec(code, run_globals)
File "/usr/src/app/__main__.py", line 12, in <module>
main()
File "/usr/src/app/__main__.py", line 6, in main
db = firestore.Client(credentials=AnonymousCredentials())
File "/usr/local/lib/python3.10/site-packages/google/cloud/firestore_v1/client.py", line 94, in __init__
super(Client, self).__init__(
File "/usr/local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_client.py", line 125, in __init__
super(BaseClient, self).__init__(
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 318, in __init__
_ClientProjectMixin.__init__(self, project=project, credentials=credentials)
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 266, in __init__
project = self._determine_default(project)
File "/usr/local/lib/python3.10/site-packages/google/cloud/client/__init__.py", line 285, in _determine_default
return _determine_default_project(project)
File "/usr/local/lib/python3.10/site-packages/google/cloud/_helpers/__init__.py", line 154, in _determine_default_project
_, project = google.auth.default()
File "/usr/local/lib/python3.10/site-packages/google/auth/_default.py", line 575, 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 h
ttps://cloud.google.com/docs/authentication/getting-started
我在 GCP 支持的帮助下解决了这个问题: 如果设置 FIRESTORE_EMULATOR_HOST,则需要说明中提到的凭据。您还需要通过环境变量 GOOGLE_CLOUD_PROJECT 设置项目 ID。该值应以“demo-”开头。例如。您可以将 GOOGLE_CLOUD_PROJECT 设置为“demo-firestore”。设置这个环境变量解决了这个问题。