简体   繁体   中英

Firestore emulator with anonymous credentials

I am trying to get Firestore working in emulator-mode with Python on my local Linux PC. 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:

First method:

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. 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

Second method:

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:

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.

Thoughts

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.

Thanks!

Method 2 works if an environment variable is set. In the following change localhost:8080 to the Firestore server address shown when the emulator is started using firebase emulators:start

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.

Also note that the emulated Firestore database will discard all its data when the server is shut down. To persist and reuse the data start the emulator with a command like this:

firebase emulators:start --import=./emulator_data --export-on-exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM