"No such file or directory" 与 firebase_admin python 图书馆
"No such file or directory" with firebase_admin python library
我想通过 firebase 数据库检索一些数据,使用 python (firebase_admin
) 的官方库而不是 pyrebase
或 python-firebase
.
我尝试执行以下代码行:
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()
但随后出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'https://project_name.firebaseio.com/.json'
即使我在浏览器中输入 url(project_name
替换为我的真实项目名称),我也从数据库中获取了 json 数据。
我该如何解决这个错误?
Certificate
应该指向一个 local 文件和你的 credentials/certificate。您改为将其指向您的数据库 URL,这不是本地文件,因此库会抛出错误。
来自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())
试试这个,这对我有用
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()
我想通过 firebase 数据库检索一些数据,使用 python (firebase_admin
) 的官方库而不是 pyrebase
或 python-firebase
.
我尝试执行以下代码行:
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()
但随后出现以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'https://project_name.firebaseio.com/.json'
即使我在浏览器中输入 url(project_name
替换为我的真实项目名称),我也从数据库中获取了 json 数据。
我该如何解决这个错误?
Certificate
应该指向一个 local 文件和你的 credentials/certificate。您改为将其指向您的数据库 URL,这不是本地文件,因此库会抛出错误。
来自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())
试试这个,这对我有用
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()