英文:
Unable to connect to firebase realtime database python code
问题
我正在尝试创建一个位置跟踪应用程序,该应用程序将从GPS获取的位置数据存储在Firebase实时数据库中,我正在编写一个可以从Firebase检索数据的Python代码。我尝试了下面的代码,但它不起作用。下面的链接包含我Firebase控制台的截图。我对Firebase是新手,无法理解我漏掉了什么,有人可以帮帮我吗?
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
if not firebase_admin._apps:
# 初始化Firebase应用
cred = credentials.Certificate("/path/to/cred.json")
firebase_admin.initialize_app(cred)
# 引用数据库中的'location'节点
ref = db.reference('/')
# 读取纬度和经度数据
latitude = ref.child('latitude').get()
longitude = ref.child('longitude').get()
# 打印纬度和经度
print("纬度:", latitude)
print("经度:", longitude)
输出是:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-60056d0e15d7> in <cell line: 12>()
10
11 # 引用数据库中的'location'节点
---> 12 ref = db.reference('/')
13
14 # 读取纬度和经度数据
1 frames
/usr/local/lib/python3.10/dist-packages/firebase_admin/db.py in reference(path, app, url)
67 """
68 service = _utils.get_app_service(app, _DB_ATTRIBUTE, _DatabaseService)
---> 69 client = service.get_client(url)
70 return Reference(client=client, path=path)
71
/usr/local/lib/python3.10/dist-packages/firebase_admin/db.py in get_client(self, db_url)
796
797 if not db_url or not isinstance(db_url, str):
--> 798 raise ValueError(
799 'Invalid database URL: "{0}". Database URL must be a non-empty '
800 'URL string.'.format(db_url))
ValueError: Invalid database URL: "None". Database URL must be a non-empty URL string.[This is structure of my data](https://i.stack.imgur.com/osWBR.png)
英文:
I'm trying to create a location tracking application that stores location data obtained from GPS in the Firebase real-time database, and I'm writing a Python code that can retrieve data from Firebase. I have tried using the below code but it's not working. The below link has a screenshot of my Firebase console. I'm new to Firebase and I am unable to understand what I am missing can anyone please help me?
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
if not firebase_admin._apps:
# Initialize the Firebase app
cred = credentials.Certificate("/path/to/cred.json")
firebase_admin.initialize_app(cred)
# Reference to the 'location' node in the database
ref = db.reference('/')
# Read latitude and longitude data
latitude = ref.child('latitude').get()
longitude = ref.child('longitude').get()
# Print the latitude and longitude
print("Latitude:", latitude)
print("Longitude:", longitude)
The output is
`---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-1-60056d0e15d7> in <cell line: 12>()
10
11 # Reference to the 'location' node in the database
---> 12 ref = db.reference('/')
13
14 # Read latitude and longitude data
1 frames
/usr/local/lib/python3.10/dist-packages/firebase_admin/db.py in reference(path, app, url)
67 """
68 service = _utils.get_app_service(app, _DB_ATTRIBUTE, _DatabaseService)
---> 69 client = service.get_client(url)
70 return Reference(client=client, path=path)
71
/usr/local/lib/python3.10/dist-packages/firebase_admin/db.py in get_client(self, db_url)
796
797 if not db_url or not isinstance(db_url, str):
--> 798 raise ValueError(
799 'Invalid database URL: "{0}". Database URL must be a non-empty '
800 'URL string.'.format(db_url))
ValueError: Invalid database URL: "None". Database URL must be a non-empty URL string.This is structure of my data`
答案1
得分: 0
你需要告诉Firebase SDK要访问的数据库的URL是什么。从Firebase的入门文档中提取以下代码:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# 获取服务帐户密钥JSON文件内容
cred = credentials.Certificate('path/to/serviceAccountKey.json')
# 使用服务帐户初始化应用程序,授予管理员权限
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://databaseName.firebaseio.com' # 👈
})
# 作为管理员,该应用程序可以访问所有数据的读写权限,无论安全规则如何
ref = db.reference('restricted_access/secret_document')
print(ref.get())
你遗漏了我用👈
标记的部分。你可以在Firebase控制台中找到数据库的URL。
英文:
You need to tell the Firebase SDK what the URL is for the database that you want it to access. From the Firebase documentation on getting started:
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, regradless of Security Rules
ref = db.reference('restricted_access/secret_document')
print(ref.get())
You're missing the part that I marked with 👈
. You can find the URL for your database in the Firebase console.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论