thisbejim / Pyrebase

A simple python wrapper for the Firebase API.
2.06k stars 527 forks source link

Stream auth timeout error even with Service Account Credentials #224

Open antoinezambelli opened 6 years ago

antoinezambelli commented 6 years ago

I'm getting the following error on my stream after a while of it being up, despite having setup the Service Account Credentials.

raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://****.firebaseio.com/****.json

It seems to be a timeout issue. I've seen a few issues here about that but they all indicate it should be fixed with SAC. Note that I've only encountered this issue when there is no activity on the stream (left it open after stopping dev work for the day). I haven't had constant activity for very long at a time, so maybe it happens in that case as well.

Here is my code in case I messed up the setup:

def listen_changes(message):
    if message["path"] != '/':
    threading.Thread(target=my_func,args=[message[0],message[1]]).start()

config = {"apiKey": "Server Key found in Settings>Cloud Messaging",
"authDomain":"****.firebaseapp.com",
"databaseURL":"https://****.firebaseio.com",
"storageBucket":"****.appspot.com",
"serviceAccount":"****-firebase-adminsdk-****.json"}

firebase = pyrebase.initialize_app(config)
auth = firebase.auth()

db = firebase.database()
dstor = firebase.storage()

submit_stream = db.child("submit_data").stream(listen_changes)

Some thoughts off the bat: 1) I actually forgot to add the apiKey for a while but it still worked (with timeout issues). Should that apiKey be the Server Key or the Web API key found in Settings>General in the Firebase console?

2) I could always close and reopen the stream on a timer...but that seems ugly.

3) Is there a way to catch that HTTPError so I only reopen the stream if/when it goes down?

4) Is a simple ping needed to keep the stream active? Like I said I haven't tested this with activity throughout.

I'm hoping I just messed something up and there's actually an easy fix.

antoinezambelli commented 6 years ago

Getting new behavior...quite likely I missed a git pull somewhere along the way and adding the server key in fixed it. I'll reopen if I can reproduce under current conditions.

antoinezambelli commented 6 years ago

Behavior still present...though it has now managed to reconnect at least once on its own before timing out.

antoinezambelli commented 6 years ago

I can confirm that closing and reopening the stream doesn't help.

antoinezambelli commented 6 years ago

Is there any easy way to catch the timeout exception? Setting the entire firebase auth procedure in a timer thread works to reconnect - but if we get a redirect error (instead of auth error) then the thread is dead until the timer closes and reopens everything?

NsrWissam commented 5 years ago

@antoinezambelli did you eventually find a way to solve the timeout problem? And if so, how?

antoinezambelli commented 5 years ago

If memory serves, I never resolved it. Trying to do a fully python firebase app was riddled with problems (for me). I eventually dropped the python firebase structure, and just did everything with a javascript server calling python scripts - it proved to be a really stable way of doing things (in my case). Mind you, this was a couple years ago so maybe something's improved.