Outcast
Outcast

Reputation: 5117

"No such file or directory" with firebase_admin python library

I want to retrieve some data by a firebase database with using the official library for python (firebase_admin) instead of pyrebase or python-firebase.

I try to execute the following lines of code:

from firebase_admin import db
from firebase_admin import credentials
import firebase_admin

cred = credentials.Certificate('https://project_name.firebaseio.com/.json')
firebase_admin.initialize_app(cred)

result = db.Query.get()

but then I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'https://project_name.firebaseio.com/.json'

even though when I enter this url on my browser (with project_name replaced with my real project name) I am getting the json of data from the database.

How can I fix this error?

Upvotes: 1

Views: 5405

Answers (2)

Ramith K S
Ramith K S

Reputation: 107

Try this ,this works for me

import os
from firebase_admin import credentials, firestore, initialize_app

# Initialize Firestore DB
data = os.path.abspath(os.path.dirname(__file__)) + "/serviceAccountKey.json"
cred = credentials.Certificate(data)
default_app = initialize_app(cred)
db = firestore.client()

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

The Certificate should point to a local file with your credentials/certificate. You are instead pointing it to your database URL, which is not a local file, so the library throws an error.

From the documentation on initializing the Python SDK:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Fetch the service account key JSON file contents
cred = credentials.Certificate('path/to/serviceAccountKey.json')

# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://databaseName.firebaseio.com'
})

# As an admin, the app has access to read and write all data, regardless of Security Rules
ref = db.reference('restricted_access/secret_document')
print(ref.get())

Upvotes: 2

Related Questions