ZoneMinder / pyzm

Python API, Log, Event Server and Memory wrapper for ZoneMinder
GNU General Public License v2.0
31 stars 20 forks source link

Problem with Token after sometime #13

Closed pedromoraesh closed 3 years ago

pedromoraesh commented 3 years ago

Hello Guys,

I have a routine coded with scheduler and twice a day i Start 7 AM and Stop 10 AM my monitors using:

m.set_parameter({'enabled': False, 'function': "None"})

Then i getting the error:

Traceback (most recent call last):
  File "/home/myuser/.local/lib/python3.6/site-packages/pyzm/api.py", line 252, in _make_request
    r.raise_for_status()
  File "/home/myuser/.local/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: http://192.168.15.5/zm/api/monitors/1.json?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIzM$

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "routines.py", line 64, in <module>
    schedule.run_pending()
  File "/home/myuser/.local/lib/python3.6/site-packages/schedule/__init__.py", line 563, in run_pending
    default_scheduler.run_pending()
  File "/home/myuser/.local/lib/python3.6/site-packages/schedule/__init__.py", line 94, in run_pending
    self._run_job(job)
  File "/home/myuser/.local/lib/python3.6/site-packages/schedule/__init__.py", line 147, in _run_job
    ret = job.run()
  File "/home/myuser/.local/lib/python3.6/site-packages/schedule/__init__.py", line 466, in run
    ret = self.job_func()
  File "routines.py", line 38, in stop_video_recording
    m.set_parameter({'enabled': False, 'function': "None"})
  File "/home/myuser/.local/lib/python3.6/site-packages/pyzm/helpers/Monitor.py", line 158, in set_parameter
    return self.api._make_request(url=url, payload=payload, type='post')
  File "/home/myuser/.local/lib/python3.6/site-packages/pyzm/api.py", line 258, in _make_request
    self._relogin()
  File "/home/myuser/.local/lib/python3.6/site-packages/pyzm/api.py", line 134, in _relogin
    self._login()

In a space of 1 hour, the error doesn't occur. In order to reproduce the error i used a space of 3 hours between turn ON and OFF the monitor!

Seems like token is expiring and can't perform calls to API, because before this error Unauthorized operation exception occurs.

I did the operation with 1 Hour manually in python3 terminal and everything worked, like images below. I think is something with the Token Refresh Operation!

AUTH_RELAY is set to hashed A valid AUTH_HASH_SECRET is provided (not empty) AUTH_HASH_IPS is disabled OPT_USE_APIS is enabled

image

image

image

I found something my be related to this ISSUE: https://github.com/ZoneMinder/zoneminder/issues/2866

ZM Version 1.34.16

pliablepixels commented 3 years ago

Could you post debug logs when this occurs? You've only posted the exception traceback.

pedromoraesh commented 3 years ago

@pliablepixels

[INFO] Using simple log output (default)
[DEBUG 1] Warning, SSL certificate check has been disbled
[DEBUG 1] using username/password for login
[DEBUG 1] Using new token API
[DEBUG 1] Refresh token expires on:2020-08-10 21:32:03.151076 [86400s]
[DEBUG 1] make_request called with url=http://192.168.15.5/zm/api/host/gettimezone.json payload={} type=get query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3$
[INFO] Using simple log output (default)
[DEBUG 1] Retrieving monitors via API
[DEBUG 1] make_request called with url=http://192.168.15.5/zm/api/monitors.json payload={} type=get query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIzMTIz$
[INFO] Using simple log output (default)
[DEBUG 1] Retrieving config via API
[DEBUG 1] make_request called with url=http://192.168.15.5/zm/api/configs.json payload={} type=get query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIzMTIzL$
[Every 1 day at 21:35:00 do start_video_recording() (last run: [never], next run: 2020-08-09 21:35:00), Every 1 day at 08:00:00 do createJob() (last run: [never], next run: 2020-08-10 08:00:00), Every 1 $
Start Recording Videos 08/09/2020 - 21:35:00
[DEBUG 1] make_request called with url=http://192.168.15.5/zm/api/monitors/1.json payload={'Monitor[Function]': 'Record', 'Monitor[Enabled]': '1'} type=post query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJI$
Stopping Videos 08/09/2020 - 23:40:00
[DEBUG 1] make_request called with url=http://192.168.15.5zm/api/monitors/1.json payload={'Monitor[Function]': 'None', 'Monitor[Enabled]': '0'} type=post query={'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUz$
[DEBUG 1] Got API access error: 401 Client Error: Unauthorized for url: http://192.168.15.5/zm/api/monitors/1.json?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIz$
[DEBUG 1] Retrying login once
[DEBUG 1] Going to use refresh token as it still has 1312.0474727666667 minutes remaining
[DEBUG 1] Using token for login [eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIzMTIzLCJleHAiOjE1OTcxMDk1MjMsInVzZXIiOiJmbG93aXgiLCJ0eXBlIjoicmVmcmVzaCJ9.KXEaVciXR4sKL86a$
[DEBUG 1] Using new token API
Traceback (most recent call last):
  File "/home/myuser/.local/lib/python3.6/site-packages/pyzm/api.py", line 252, in _make_request
    r.raise_for_status()
  File "/home/myuser/.local/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: http://192.168.15.5/zm/api/monitors/1.json?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJab25lTWluZGVyIiwiaWF0IjoxNTk3MDIzM$
pliablepixels commented 3 years ago

Confirmed, will see what is going on.

pliablepixels commented 3 years ago

I've published an updated version of pyzm. Please try it out - seems to work for me after 1-2 hour gaps

pedromoraesh commented 3 years ago

Trying right know. Did you update the pip version ? Or from source?

Putting script to run with scheduler for 8 hours.

pliablepixels commented 3 years ago

Yes, the package is updated

pedromoraesh commented 3 years ago

@pliablepixels 3 days of running, no problems for now!

pliablepixels commented 3 years ago

sounds good. I suppose we can close for now. If it reappears, we can reopen.