我正在使用以下代码从Raspberry pi上的Firebase获取数据,但它显示以下错误。但是我可以在没有身份验证的情况下获取数据。
from firebase.firebase import FirebaseApplication
from firebase.firebase import FirebaseAuthentication
firebase = firebase.FirebaseApplication('https://myapp.firebaseio.com/',
authentication =None)
authentication = firebase.Authentication('secretkey',
'prateekrai266@gmail.com', extra={'id': 123})
firebase.authentication = authentication
print (authentication.extra)
user = authentication.get_user()
print (user.firebase_auth_token)
result = firebase.get('/messages', None)
它显示以下错误
追踪(最近一次呼叫最后一次):
文件“/home/pi/code/dataauth.py”,第7行,在authentication =中 firebase.Authentication('secretkey','prateekrai266 @ gmail.com', extra = {'id':123})AttributeError:'FirebaseApplication'对象没有 属性'身份验证'
我能够在没有身份验证的情况下获取数据,即通过遵循代码
将规则设置为truefrom firebase.firebase import FirebaseApplication
firebase = firebase.FirebaseApplication('https://myapp.firebaseio.com/',
None)
result = firebase.get('/messages', None)
答案 0 :(得分:0)
我认为您使用的是python-firebase,它有点弃用,并且不会长时间更新。我已经尝试解决您的问题,为您的问题找到解决方案,但仍然会出现身份验证问题。
代码:
from firebase.firebase import FirebaseApplication
from firebase.firebase import FirebaseAuthentication
DSN = config['databaseURL'] # 'https://myapp.firebaseio.com/'
SECRET = config['userPass'] # 'secretkey'
EMAIL =config['userEmail'] # 'prateekrai266@gmail.com'
authentication = FirebaseAuthentication(SECRET,EMAIL, True, True)
firebase = FirebaseApplication(DSN, authentication)
firebase.get('/messages', None)
我建议进入pyrebase
,这是更新的,因为我刚检查了作品。
https://github.com/thisbejim/Pyrebase
适用于我的代码:
import pyrebase
config = {
'apiKey': "YYY",
'authDomain': "XXXXXXXX.firebaseapp.com",
'databaseURL': "https://XXXXXXXX.firebaseio.com",
'projectId': "XXXXXXXX",
'storageBucket': "XXXXXXXX.appspot.com",
'messagingSenderId': "ZZZ",
}
firebase = pyrebase.initialize_app(config)
userEmail = 'youremailadresthatyouaddedtofirebasedatabaseauthenticatedusers'
userPass = 'yourpasswordsetupforthisemail'
auth = firebase.auth()
# Log the user in
user = auth.sign_in_with_email_and_password(userEmail, userPass)
# Get a reference to the database service
db = firebase.database()
# data to save
data = {
"name": "Mortimer 'Morty' Smith"
}
# Pass the user's idToken to the push method
results = db.child("test").push(data, user['idToken'])
print results
results = db.child("test").get(user['idToken'])
print results.val()