1

i'm trying to use firebase in colab with Python. But there is unsolvable error, so i need some help.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


cred = credentials.Certificate('/content/myKey.json')
firebase_admin.initialize_app(cred) # error in this line
db = firestore.client()

ValueError: : The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.

What can i do for solving this problem?

i also found similar answer with this, so i tried some many tips in there, like below.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

if not firebase_admin._apps:
  cred = credentials.Certificate('/content/foodle-94e80-firebase-adminsdk-zr21t- f02504e9fb.json')
  firebase_admin.initialize_app(cred)
else:
  app = firebase_admin.get_app()

db = firestore.client(app) # new error in this line

but new error is confusing me.

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

What can i do?

2 Answers 2

1

Looks like there's a default instance of the Firebase app getting initialized somewhere. When the default instance gets created, it uses GOOGLE_APPLICATION_CREDENTIALS instead of the credentials you pass in manually. You can either provide GOOGLE_APPLICATION_CREDENTIALS to the script, or ignore the default instance of the firebase app and create an explicitly named one.

To create an explicitly named app, change your code to provide a name:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


cred = credentials.Certificate('/content/myKey.json')
firebase_admin.initialize_app(credential=cred, name='myApp') 
db = firestore.client()

To provide GOOGLE_APPLICATION_CREDENTIALS and use the default app:

If you're running your python script from the console, you can provide a value for that by running

export GOOGLE_APPLICATION_CREDENTIALS='/content/myKey.json'

In colab, you need to add this to your script:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/content/myKey.json"

After this you can run your second example.

(To get the credentials JSON go to this page, select your firebase-adminsdk service account, click 'ADD KEY', 'Create new key', select JSON as your option and download the resulting file.)

Sign up to request clarification or add additional context in comments.

Comments

0

In my case it worked with

cred = credentials.Certificate("/content/drive/My Drive/Colab Notebooks/LALALA.json")
firebase_admin.initialize_app(cred)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.