nhorvath / Pyrebase4

A simple python wrapper for the Firebase API. ⛺
254 stars 63 forks source link

Service Account gets 401 Client Error after 1+ hours #3

Open jasonshah opened 5 years ago

jasonshah commented 5 years ago

Make sure these boxes are checked before submitting your issue:

[x] Check that your version of Python is 3.4+ [x] Check that you are on the newest version of Pyrebase [x] Check that Email/password provider is enabled in your Firebase dashboard under Auth -> Sign In Method.

Hi, I'm using Pyrebase4 for a project that, at its core, does nothing but waits for and reacts to changes to two Firebase database variables. I've created a Service Account for this, per the readme, and the configuration seems to work. The Service Account has the Editor role per the GCP IAM console for this project.

After 1+ hours, I get a 401 error:

Exception` in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/usr/local/lib/python3.5/dist-packages/pyrebase/pyrebase.py", line 619, in start_stream for msg in self.sse: File "/usr/local/lib/python3.5/dist-packages/pyrebase/pyre_sseclient.py", line 85, in next self._connect() File "/usr/local/lib/python3.5/dist-packages/pyrebase/pyrebase.py", line 584, in _connect super(ClosableSSEClient, self)._connect() File "/usr/local/lib/python3.5/dist-packages/pyrebase/pyre_sseclient.py", line 54, in _connect self.resp.raise_for_status() File "/usr/local/lib/python3.5/dist-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://powerswitch-4d391.firebaseio.com/shouldTakeAndUploadGaragePicture.json

This locks up the application and it cannot continue.

I looked into trying to refresh the Service Account token, but I don't see a way to do that.

Any suggestions on how to proceed? Thanks!

jasonshah commented 5 years ago

Note: I also believe I've received 403 errors after 1+ hours too, but I don't exactly recall.

nhorvath commented 5 years ago

Your token probably times out. You should catch and handle the error message by logging in again to get a new token.

jasonshah commented 5 years ago

I would do that, but I don't see how to get a new token for a service account. Do you have any suggestions?

nhorvath commented 5 years ago

you should perform all authorization steps you did originally when your program starts again.

On Fri, May 17, 2019 at 10:23 AM Jason Shah notifications@github.com wrote:

I would do that, but I don't see how to get a new token for a service account. Do you have any suggestions?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/nhorvath/Pyrebase4/issues/3?email_source=notifications&email_token=AABIJXC6BA7OWZNOKVDZRHTPV25WNA5CNFSM4HNULC6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVU4UVQ#issuecomment-493472342, or mute the thread https://github.com/notifications/unsubscribe-auth/AABIJXF4YWQ7E77PN54CDEDPV25WNANCNFSM4HNULC6A .

jasonshah commented 5 years ago

I have two lines near the start like this:

firebase = pyrebase.initialize_app(config)
db = firebase.database();

and that's pretty much it when it comes to authorization.

Would it re-running authorization steps consist of just re-running the first line (or first two) lines every <1 hour? Or is there something else that triggers auth for service accounts that you know of?

Thanks again!

nhorvath commented 5 years ago

doing initialize_app again should re-init with your service credentials. I wouldn't just do it automatically on a schedule. I would catch the error and retry.

jasonshah commented 5 years ago

Nick, What I don't understand is, where do I actually catch this exception?

pyrebase's Stream initializes a thread, and the exception occurs the thread. There is no hook into my code to be able to catch the exception per the traceback (unless there is a Pythonic way to do this that I don't know about).

nhorvath commented 5 years ago

Yeah I see what you mean now. I'm not sure of a good way to handle this.

phishboh commented 4 years ago

Related? https://stackoverflow.com/questions/55876107/how-to-detect-realtime-listener-errors-in-firebase-firestore-database

HarsheetKakar commented 4 years ago

Nick, What I don't understand is, where do I actually catch this exception?

pyrebase's Stream initializes a thread, and the exception occurs the thread. There is no hook into my code to be able to catch the exception per the traceback (unless there is a Pythonic way to do this that I don't know about).

stream was a big issue in the original library also.