I am using the python-firebase library by Ozgur Vatansever for my program.
I have previously been able to write/read data to my firebase, however with the introduction of authentication I cannot. Using:
from firebase import firebase
auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com')
myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', auth)
result = myFirebase.get('/users', None)
print result
Resulting in:
HTTPError: 400 Client Error: Bad Request
Conversely, using no authentication:
myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', None)
result = myFirebase.get('/users', None)
works as expected. Any help in why I am getting this HTTP error: 400
, when using authentication is greatly appreciated.
I'm using version 1.2 of the library and python 2.7
In here there's an example on how to connect easily using auth.
import datetime
from firebase.firebase import FirebaseApplication, FirebaseAuthentication
if __name__ == '__main__':
SECRET = '12345678901234567890'
DSN = 'https://firebase.localhost'
EMAIL = 'your@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)
I found the same problem, and the problem seems to be that Firebase is rejecting the token that python-firebase builds (in FirebaseTokenGenerator).
I don't know what is wrong with the token being generated, and given that I am happy to use the JWT token provided in the Secrets section of the Firebase Dashboard, this hack works for me:
In firebase.py, class FirebaseAuthentication, right at the start of the get_user method, add an if clause:
def get_user(self):
if self.extra.get('explicitAuth') is None:
token = self.authenticator.create_token(self.extra)
else:
token = self.extra.get('explicitAuth')
Then when invoking the application provide the explicit token overriding the generated one:
auth = firebase.FirebaseAuthentication('Ignore', 'Ignore', extra={'explicitAuth': myToken})
app = firebase.FirebaseApplication('https://mystuff.firebaseio.com/', auth)
Maybe you should have a look at the docs Custom Authentication
The library you are using takes python-firebase to generate tokens. In your code,
auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com')
it should be:
auth = firebase.FirebaseAuthentication('MY_FIREBASE_SECRET','myEmail@email.com', extra={'uid': '123'})
The param extra
is the payload to create token and uid
is a must as the github page says.
look at the file firebase.py
, class FirebaseApplication
function _authenticate
:
replace 'auth'
by 'auth_token'
in params.update({'auth_token': user.firebase_auth_token})
fixes this HTTPError: 400 Client Error: Bad Request
error but don't fix all auth problems...
Let's try this, Go to Rules tab and set true
{
"rules": {
".read": "true",
".write": "true"
}
}
Code from firebase import firebase
myFirebase = firebase.FirebaseApplication('https://MYFIREBASE.firebaseio.com', None)
result = myFirebase.get('/users', None)
print result
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.