Firebase user authentication in python

I am trying to get firebase data from python (2.7).

Here are my rules (on firebseio.com):

{ "rules": { "user": { "$uid": { ".read": "auth != null && auth.uid == $uid", ".write": "auth != null && auth.uid == $uid" } } } } 

Here is a screenshot of my databse:

enter image description here

And finally, my python code:

 from firebase import firebase from firebase.firebase import FirebaseApplication, FirebaseAuthentication DSN = 'https://<my name>.localhost' EMAIL = ' qqq@qqq.qqq ' authentication = FirebaseAuthentication(EMAIL, True, True, extra={'id': '<the user id>'}) firebase = FirebaseApplication(DSN, authentication) firebase.authentication = authentication print authentication.extra user = authentication.get_user() print user.firebase_auth_token 

Now I can’t figure out how to get data and send data to and from firebase. I tried using the line: result = firebase.get('/users', None, {'print': 'pretty'}) , but it gives me this error:

 ConnectionError: HTTPSConnectionPool(host='<my name>.localhost', port=443): Max retries exceeded with url: /users/.json?print=pretty&auth=<the token code of the user> (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x02A913B0>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',)) 

Can someone provide me with working code?

Thanks in advance,

Zvi Carp

+5
source share
1 answer

Here are the steps we took to make authentication work.

(1) First you need a Firebase secret. Once you have a project in Firebase, click on "Settings." Then click on "Database" and select "Create Secret." settings

Copy your secret. It will go into your code later.

secret

(2) You need the URL of your Firebase. It will have the following format: https: //.firebaseio.com Copy this too.

(3) Get the Firebase REST API for Python. We used this: https://github.com/benletchford/python-firebase-gae Go to the lib directory and run this command that will put the firebase code in your lib directory:

 git clone http://github.com/benletchford/python-firebase-gae lib/firebase 

(4) In your "main.py" file (or whatever you use) add this code:

 from google.appengine.ext import vendor vendor.add('lib') from firebase.wrapper import Firebase FIREBASE_SECRET = 'YOUR SECRET FROM PREVIOUS STEPS' FIREBASE_URL = 'https://[…].firebaseio.com/' 

(5) Add this code to MainHandler (if you are using AppEngine):

 class MainHandler(webapp2.RequestHandler): def get(self): fb = Firebase(FIREBASE_URL + 'users.json', FIREBASE_SECRET) new_user_key = fb.post({ "job_title": "web developer", "name": "john smith", }) self.response.write(new_user_key) self.response.write('<br />') new_user_key = fb.post({ "job_title": "wizard", "name": "harry potter", }) self.response.write(new_user_key) self.response.write('<br />') fb = Firebase(FIREBASE_URL + 'users/%s.json' % (new_user_key['name'], ), FIREBASE_SECRET) fb.patch({ "job_title": "super wizard", "foo": "bar", }) fb = Firebase(FIREBASE_URL + 'users.json', FIREBASE_SECRET) self.response.write(fb.get()) self.response.write('<br />') 

Now, when you go to your Firebase Realtime database, you should see entries for Harry Potter as a user and others.

+6
source

Source: https://habr.com/ru/post/1246356/


All Articles