Accessing API using Python requests
Posted: Sun Jun 17, 2018 4:25 pm
Hi everyone,
I'm trying to use python to access the API {"version":"1.30.4","apiversion":"1.0"}. I'm able to successfully use curl from the examples on https://zoneminder.readthedocs.io/en/stable/api.html. However, I can't seem to get past logging in using Python 3.6 with request module.
This works:
s.cookies output is:
Trying to get the monitor list returns unauthorized response.
There must be some difference between how curl provides the cookie and how python requests provides it. I'm hoping someone else has figured this out already. I've use APIs with api keys, but have never had to maintain cookies, so this is new for me and just not sure what to do to get this to work.
Thanks,
Ryan
I'm trying to use python to access the API {"version":"1.30.4","apiversion":"1.0"}. I'm able to successfully use curl from the examples on https://zoneminder.readthedocs.io/en/stable/api.html. However, I can't seem to get past logging in using Python 3.6 with request module.
This works:
Code: Select all
# Python 3.6
import requests # version 2.18.4
s = requests.Session()
header = {"username":"admin",
"password":"XXXX",
"action":"login",
"view":"console"}
s.get("http://localhost/zm/index.php", headers=header)
The object s should be maintaining the cookie, but, unfortunately, I'm not successful from here.<RequestsCookieJar[Cookie(version=0, name='ZMSESSID', value='dv9l1p4fdh4ndhdpjeuarxxxxx', port=None, port_specified=False, domain='localhost.local', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False), Cookie(version=0, name='zmCSS', value='flat', port=None, port_specified=False, domain='localhost.local', domain_specified=False, domain_initial_dot=False, path='/zm', path_specified=False, secure=False, expires=1840288943, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False), Cookie(version=0, name='zmSkin', value='classic', port=None, port_specified=False, domain='localhost.local', domain_specified=False, domain_initial_dot=False, path='/zm', path_specified=False, secure=False, expires=1840288943, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False)]>
Trying to get the monitor list returns unauthorized response.
Code: Select all
s.get("http://localhost/zm/api/monitors.json")
<Response [401]>
Thanks,
Ryan