0
votes

I need to connect with firestore database using python, but I don't want to use Service Account Key. Can I connect using python just with ProjectId and ApiKey?

Using javascript I can do that just like that:

firebase.initializeApp({
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
});

But I can't do that using Python I get this error:

self._g_credential, self._project_id = google.auth.default(scopes=_scopes)
  File "/home/caique/.local/lib/python2.7/site-packages/google/auth/_default.py", line 306, 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.

This is my code right now:

import firebase_admin
from firebase_admin import credentials, firestore, auth

cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': "my-project-id",
'apiKey': "my-api-key"
})

db = firestore.client()
1

1 Answers

0
votes

You have to use a service account to connect. This is what authorizes your code to access Firestore and other Google Cloud resources in your project.

The only time you don't need to provide a service account is when your code is running in a secure Google Cloud environment, such as Cloud Functions or App Engine. In that case, Google Cloud libraries will automatically pick up default service account credentials from the execution environment. But when running on your own machine, you need to initialize with a service account.