Authenticated REST API data calls in Python

864 views
Skip to first unread message

Marcio Valenzuela

unread,
Sep 28, 2016, 10:37:18 AM9/28/16
to Firebase Google Group
Im trying to fetch some data from my app.

I've so far managed to fetch it from a javascript app by using a database initialization config code.  Im assuming that's one way of doing it, but I only know how to do it in javascript:

<script src="https://www.gstatic.com/firebasejs/3.4.0/firebase.js"></script>
<script>
 
// Initialize Firebase
 
var config = {
    apiKey
: "mykey",
    authDomain
: "myapp.firebaseapp.com",
    databaseURL
: "https://myapp.firebaseio.com",
    storageBucket
: "myapp.appspot.com",
    messagingSenderId
: "698532324181"
 
};
  firebase
.initializeApp(config);
</script>


Then I use the signInWithEmailAndPassword() and then a ref.on('value') call.

I want to do it in Python and Im a bit confused by the changes in versions etc.  So far I seem to have found 2 ways of doing it, but I dont know how to use either:

from firebase import firebase
from firebase_token_generator import create_token
from firebase import FirebaseAuthentication // but this is greyed out in PyCharm for some reason


#Method A
firebase
= firebase.FirebaseApplication('https://myapp.firebaseio.com')
authentication
= firebase.Authentication('mypwd', 'myemail', extra={'id': 123})
firebase
.authentication = authentication
print authentication.extra

user
= authentication.get_user()
print user.firebase_auth_token

result
= firebase.get('/timeoff', None)
print result


but when running it on PyCharm I get:
AttributeError: 'FirebaseApplication' object has no attribute 'Authentication'

#Method B
auth_payload = { "uid": "uniqueId1", "auth_data": "foo", "other_auth_data": "bar" }
token
= create_token("<YOUR_FIREBASE_SECRET>", auth_payload)


but I dont know how to use this and I cant find my firebase secret.

Please help!

Daniel Cortés

unread,
Nov 28, 2016, 4:08:01 PM11/28/16
to Firebase Google Group

In the git repo there's an example that helps a lot, the link is here.
The code is the following:
import datetime

from firebase.firebase
import FirebaseApplication, FirebaseAuthentication


if __name__ == '__main__':
    SECRET
= '12345678901234567890'
DSN
= 'https://firebase.localhost'
EMAIL
= 'yo...@email.com'
authentication
= FirebaseAuthentication(SECRET, EMAIL, True, True)
firebase
= FirebaseApplication(DSN, authentication)

firebase
.get('/users', None,
   
params = {
       
'print': 'pretty'
   
},
    headers
= {
       
'X_FANCY_HEADER': 'very fancy'
   
})

data
= {
   
'name': 'Ozgur Vatansever',
   
'age': 26,
   
'created_at': datetime.datetime.now()
}

snapshot
= firebase.post('/users', data)
print(snapshot['name'])

def callback_get(response):
   
with open('/dev/null', 'w') as f:
    f
.write(response)
firebase
.get_async('/users', snapshot['name'], callback = callback_get)


This is a demo app so no issue with the secret being public

Hope this helps!
Reply all
Reply to author
Forward
0 new messages