[英]Firestore emulator with anonymous credentials
I am trying to get Firestore working in emulator-mode with Python on my local Linux PC.我正在尝试让 Firestore 在本地 Linux PC 上使用 Python 在模拟器模式下工作。 Is it possible to use anonymous credentials for this so I don't have to create new credentials?
是否可以为此使用匿名凭据,这样我就不必创建新的凭据?
I have tried two methods of getting access to the Firestore database from within a Python Notebook, after having run firebase emulators:start
from the command-line:在运行
firebase emulators:start
:
from firebase_admin import credentials, firestore, initialize_app
project_id = 'my_project_id'
cred = credentials.ApplicationDefault()
initialize_app(cred, {'projectId': project_id})
db = firestore.client()
This raises the following exception:这引发了以下异常:
DefaultCredentialsError: Could not automatically determine credentials.
DefaultCredentialsError:无法自动确定凭据。 Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application.
请设置 GOOGLE_APPLICATION_CREDENTIALS 或明确创建凭据并重新运行应用程序。 For more information, please see https://cloud.google.com/docs/authentication/getting-started
有关详细信息,请参阅https://cloud.google.com/docs/authentication/getting-started
from google.auth.credentials import AnonymousCredentials
from google.cloud.firestore import Client
cred = AnonymousCredentials()
db = Client(project=project_id, credentials=cred)
This works, but then I try and access a document from the database that I have manually inserted into the database using the web-interface, using the following Python code:这行得通,但随后我尝试使用以下 Python 代码从数据库中访问我已使用 Web 界面手动插入到数据库中的文档:
doc = db.collection('my_collection').document('my_doc_id').get()
And then I get the following error, which perhaps indicates that the anonymous credentials don't work:然后我收到以下错误,这可能表明匿名凭据不起作用:
PermissionDenied: 403 Missing or insufficient permissions.
PermissionDenied:403 缺少或权限不足。
It is a surprisingly complicated system and although I have read numerous doc-pages, watched tutorial videos, etc., there seems to be an endless labyrinth of configurations that need to be setup in order for it to work.这是一个非常复杂的系统,尽管我已经阅读了大量文档页面、观看了教程视频等,但似乎需要设置无穷无尽的配置迷宫才能使其正常工作。 It would be nice if I could get Firestore working on my local PC with minimal effort, to see if the database would work for my application.
如果我能以最小的努力让 Firestore 在我的本地 PC 上运行,看看数据库是否适用于我的应用程序,那就太好了。
Thanks!谢谢!
Method 2 works if an environment variable is set.如果设置了环境变量,则方法 2 有效。 In the following change
localhost:8080
to the Firestore server address shown when the emulator is started using firebase emulators:start
在以下更改
localhost:8080
到使用firebase emulators:start
时显示的 Firestore 服务器地址
import os
os.environ['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080'
I don't know how to make it work with Method 1 above using the firebase_admin
Python package. Perhaps someone else knows.我不知道如何使用
firebase_admin
Python package 使其与上面的方法 1 一起使用。也许其他人知道。
Also note that the emulated Firestore database will discard all its data when the server is shut down.另请注意,模拟的 Firestore 数据库将在服务器关闭时丢弃其所有数据。 To persist and reuse the data start the emulator with a command like this:
要持久化和重用数据,请使用如下命令启动模拟器:
firebase emulators:start --import=./emulator_data --export-on-exit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.