“没有这样的文件或目录”与firebase_admin python库

2024-04-24 23:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用python的官方库(firebase_admin)而不是pyrebase或{},通过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()

但是我得到了以下错误:

^{pr2}$

即使当我在我的浏览器上输入这个url(用project_name替换为我的真实项目名),我还是从数据库中获取数据的json。在

如何修复此错误?在


Tags: 数据namefromimportproject数据库jsondb
1条回答
网友
1楼 · 发布于 2024-04-24 23:13:45

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())

相关问题 更多 >