Python firebase_admin firestore.client() on the Firestore emulator: 'DefaultCredentialsError: Your default credentials were not found' (a Mock credential fails too); use AnonymousCredentials
Fix: give firebase_admin a credential that returns google-auth's AnonymousCredentials, or skip firebase_admin and use google-cloud-firestore directly. Both work against the emulator with no service account:
import os
os.environ["FIRESTORE_EMULATOR_HOST"] = "localhost:8080"
import firebase_admin
from firebase_admin import credentials, firestore
from google.auth.credentials import AnonymousCredentials
class EmulatorCred(credentials.Base):
def get_credential(self):
return AnonymousCredentials()
firebase_admin.initialize_app(EmulatorCred(), {"projectId": "demo-myapp"})
db = firestore.client()or from google.cloud import firestore; db = firestore.Client(project="demo-myapp"), which with FIRESTORE_EMULATOR_HOST set needs no credentials at all.
What fails (tested with firebase-admin 6.5.0 and 7.8.0, google-cloud-firestore 2.34.1, firebase-tools 15.36 emulator):
- initialize_app(options={"projectId": ...}) with no credential: firestore.client() raises DefaultCredentialsError: Your default credentials were not found.
- Mock(spec=credentials.Base), a common trick that is enough for the Auth emulator (create_user, set_custom_user_claims, list_users all work with it): firestore.client() raises ValueError: This library only supports credentials from google-auth-library-python.
So the Firestore client doesn't need real credentials, just a google-auth credentials object.