3

Google has provided nice examples for Node.js, Android and iOS authentication of clients to connect to Firebase to use Firebase Realtime Databases - but how does one connect via Python from a Google AppEngine app to a Firebase Realtime Database and authenticate properly?

1 Answer 1

5

Here are the steps we took to get this working.

(1) First you need a Firebase Secret. After you have a project in Firebase, then click Settings. Then click Database and choose to create a secret. settings

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

secret

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

(3) Get a Firebase REST API for Python. We used this one: https://github.com/benletchford/python-firebase-gae Navigate to above your lib directory and run this command which will place the firebase code into your lib directory:

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

(4) In your "main.py" file (or whatever you are using) 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 the MainHandler (assuming 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 navigate to your Firebase Realtime Database, you should see entries for Harry Potter as a user and others.

8
  • Where do you get the secret from? I cant find that option.
    – marciokoko
    Sep 28, 2016 at 1:26
  • @marciokoko It is the first step: In Firebase, then click Settings (the gear button) then click the Database tab, then choose to create a secret.
    – Praxiteles
    Sep 28, 2016 at 4:14
  • @marciokoko I just tried it. The location is still there.
    – Praxiteles
    Sep 28, 2016 at 4:14
  • OH! I was going to the left hand side bar "Database" link instead of the top tab! Thanks so much!
    – marciokoko
    Sep 28, 2016 at 14:29
  • @Praxiteles I tried this, however it only will post the john smith entry. It will then throw an error for me: AttributeError: 'NoneType' object has no attribute 'write' This is from the first self.response.write(new_user_key)
    – DarkMatter
    Jan 21, 2017 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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