2

I'm using the following code to fetch data from Firebase on Raspberry Pi but it shows following error. However I'm able to fetch data without authentication.

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)

it shows following error

Traceback (most recent call last):

File "/home/pi/code/dataauth.py", line 7, in authentication = firebase.Authentication('secretkey', 'prateekrai266@gmail.com', extra={'id': 123}) AttributeError: 'FirebaseApplication' object has no attribute 'Authentication'

I'm able to fetch data without authentication i.e. setting rules to true, by following code

from firebase.firebase import FirebaseApplication
firebase = firebase.FirebaseApplication('https://myapp.firebaseio.com/', 
None)
result = firebase.get('/messages', None)
2

You are making a couple of mistakes that really confuse the Python interpreter, although it looks like you probably read this documentation, which contains completely broken code. I think it's unlikely that the code there has actually been tested before publishing, because there are a couple of mistakes:

  1. You can't declare a variable with the same name as a module you import.

    firebase = firebase.FirebaseApplication('https://myapp.firebaseio.com/', authentication =None)
    

    That line redeclares the module firebase as a variable of type FirebaseApplication. In other words, you're stopping yourself from accessing what you tried to import later on, with the line firebase.Authentication

  2. You're not importing the firebase module correctly if you did want to use the method above. You should instead only use from firebase import firebase if you intend to reference the classes as firebase.FirebaseApplication (and so on).

Take a look at the example given in the examples/ directory of their repository. This shows the correct import method and usage.

Your code should look a little more like this:

from firebase.firebase import FirebaseApplication
from firebase.firebase import FirebaseAuthentication

app = FirebaseApplication('https://myapp.firebaseio.com/', authentication=None)
authentication = FirebaseAuthentication('secretkey', 'prateekrai266@gmail.com', extra={'id': 123})
app.authentication = authentication
print (authentication.extra)
user = authentication.get_user()
print (user.firebase_auth_token)
result = app.get('/messages', None)
  • @user72225 No problem. You might like to read What should I do when someone answers my question? — on Stack Exchange, our way of saying 'thanks' is to accept an answer by clicking the 'tick' icon (which also gives you a +2 rep bonus!). – Aurora0001 Sep 13 '17 at 17:11
  • i did like your code but i cant authrize my acess . 'requests.exceptions.HTTPError: 401 Client Error: Unauthorized' What i nee dto do in firebase ocnsole to permit email auth? i enabled it already on that – Jasar Orion Jul 8 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.