Zoneminder CCTV API 2.0
Flow to log in to Zoneminder cctv server using token authentication used in API version 2.0 onwards. Uses node-red-contrib-credentials to store login username and password.
[{"id":"e8cdc7cf.2d3ff8","type":"tab","label":"Zoneminder API 2.0 Example","disabled":false,"info":""},{"id":"7e611fba.3dab3","type":"inject","z":"e8cdc7cf.2d3ff8","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":true,"onceDelay":"1","x":110,"y":100,"wires":[["7b163fdb.fa438"]]},{"id":"7b163fdb.fa438","type":"credentials","z":"e8cdc7cf.2d3ff8","name":"Zoneminder Credentials","props":[{"value":"zmip","type":"msg"},{"value":"zmuser","type":"msg"},{"value":"zmpass","type":"msg"}],"x":330,"y":100,"wires":[["f4fc0507.6482b8"]]},{"id":"c46be13.56f2e2","type":"http request","z":"e8cdc7cf.2d3ff8","name":"Post Login","method":"POST","ret":"txt","paytoqs":false,"url":"http://{{zmip}}/zm/api/host/login.json","tls":"","persist":false,"proxy":"","authType":"","x":130,"y":200,"wires":[["66dd393d.691f58","f4ddde9e.a19d5"]]},{"id":"f4fc0507.6482b8","type":"function","z":"e8cdc7cf.2d3ff8","name":"set login headers","func":"msg.payload = \"user=\"+msg.zmuser+\"&pass=\"+msg.zmpass;\nmsg.headers = {};\nmsg.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';\nmsg.headers['Accept-Encoding'] = 'gzip, deflate';\nmsg.headers['Accept-Language'] = 'en-GB,en;q=0.5';\nmsg.headers['Connection'] = 'keep-alive';\nmsg.headers['Content-type'] = 'application/x-www-form-urlencoded';\nmsg.headers['Cookie'] = 'zmSkin=classic; zmCSS=classic;';\nmsg.headers['Host'] = msg.zmip;\nmsg.headers['Referer'] = \"http://\"+msg.zmip+\"/zm/\";\nmsg.headers['Upgrade-Insecure-Requests'] = '1';\nmsg.headers['User-Agent'] = 'node-red';\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":100,"wires":[["c46be13.56f2e2"]]},{"id":"66dd393d.691f58","type":"json","z":"e8cdc7cf.2d3ff8","name":"","property":"payload","action":"obj","pretty":false,"x":310,"y":200,"wires":[["5d9fdfa0.1fb57"]]},{"id":"5d9fdfa0.1fb57","type":"change","z":"e8cdc7cf.2d3ff8","name":"Set Token","rules":[{"t":"set","p":"token","pt":"msg","to":"payload.access_token","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":200,"wires":[["8f5c5168.adf8a"]]},{"id":"8f5c5168.adf8a","type":"http request","z":"e8cdc7cf.2d3ff8","name":"Post Request","method":"POST","ret":"txt","paytoqs":false,"url":"http://{{zmip}}/zm/api/events/consoleEvents/1%20hour.json?token={{token}}","tls":"","persist":false,"proxy":"","authType":"","x":640,"y":200,"wires":[["f4ddde9e.a19d5","634a0d23.3a5644"]]},{"id":"f4ddde9e.a19d5","type":"switch","z":"e8cdc7cf.2d3ff8","name":"If No Response","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":400,"y":300,"wires":[["ac6d255b.3da638"]]},{"id":"ac6d255b.3da638","type":"change","z":"e8cdc7cf.2d3ff8","name":"API Error","rules":[{"t":"set","p":"payload","pt":"msg","to":"ZM API Error","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":300,"wires":[["24973977.b73936"]]},{"id":"634a0d23.3a5644","type":"json","z":"e8cdc7cf.2d3ff8","name":"","property":"payload","action":"obj","pretty":false,"x":830,"y":200,"wires":[["9b7e8087.170e"]]},{"id":"24973977.b73936","type":"debug","z":"e8cdc7cf.2d3ff8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":300,"wires":[]},{"id":"9b7e8087.170e","type":"debug","z":"e8cdc7cf.2d3ff8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":990,"y":200,"wires":[]},{"id":"bc3f77c3.cd7548","type":"comment","z":"e8cdc7cf.2d3ff8","name":"Change Post Login/Request if using https","info":"","x":220,"y":40,"wires":[]},{"id":"65d180d6.20fb2","type":"comment","z":"e8cdc7cf.2d3ff8","name":"Change Post Request to whatever you want from the API","info":"Currently set to get number of events in the last hour for all monitors but can be set to whatever you want.","x":610,"y":40,"wires":[]}]