Automatic auth token acquisition and flows reloading
Hello my dear friends! Many times I came across on the forum about questions about reloading flows, how to automate this, how to go through all the stages of request authorization?
I will answer all these questions in detail. I will tell you step by step how to do everything and so that you have an understanding of what you are doing.
The whole story began with the fact that I wanted to control smart amplifiers and automate the updating of information from them. Amplifiers are able to receive commands via tcp \ ip, execute them and, upon request, report on the status of certain providers. Good. I diligently wrote control functions, created all the logic of work and successfully tested it. It would seem that everything is fine - and you can install the equipment in its place and lock the controller in a cabinet. But no. The controller works constantly, but the amplifiers turn on and off. And so I found a bug. The network on the amplifiers starts before the main program is fully loaded and ready to work, so the connection established at this moment will be considered "dead". It will exist, receive data, but will not give anything. The solution is to restart the worker flows if this situation occurs.
flows can be reloaded using >>>API<<< https://nodered.org/docs/api/
To perform any action, you first need to get an authorization token based on your login and password to node-red. https://nodered.org/docs/api/admin/oauth
and only after that, using this token, you can send a restart command to your threads. https://nodered.org/docs/api/admin/methods/post/flows/
How difficult! You can say this, but just by reading the documentation and trying a little - I wrote 1 function and substituted a couple of values… Everything worked out!
You need a trigger that waits for a certain time and if it does not receive a new message, sends its own, which contains a JSON request in the msg body
Next, we have a request to get an authorization token and a function that will generate a final request for us to restart flows using our token. Below I post a test flow for you to explore. Substitute your login, password, ip address of the server. Connect data output node to trigger node AFTER TCP(or another) REQUEST node
[{"id":"774bee5a7cc47aea","type":"tab","label":"autorization request","disabled":false,"info":"","env":[]},{"id":"a3e39a4aeee055f1","type":"http request","z":"774bee5a7cc47aea","name":"reset flow req","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":410,"y":210,"wires":[["c742ab5ee2d53ef1"]]},{"id":"fcf506a8f3943a21","type":"function","z":"774bee5a7cc47aea","name":"request + token","func":"msg.url = 'http://YOUR_IP_ADDRESS:1880/flows';\nmsg.method = 'POST';\nmsg.headers = {};\nmsg.headers['content-type'] = \"application/json; charset=utf-8\";\nmsg.headers['Node-RED-Deployment-Type'] = \"reload\";\nmsg.headers['Authorization'] =\"Bearer \"+msg.payload.access_token;\nmsg.headers['Node-RED-API-Version'] = \"v2\";\nmsg.payload = {\n \"flows\": [\n {\n \"id\": \"fbbaf2cd6156b940\",\n \"type\": \"tab\",\n \"label\": \"AMP\",\n }\n ]\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":170,"wires":[["a3e39a4aeee055f1","c742ab5ee2d53ef1"]]},{"id":"672a9f73137e4e3b","type":"trigger","z":"774bee5a7cc47aea","name":"60s trig","op1":"","op2":"{\"client_id\":\"node-red-admin\",\"grant_type\":\"password\",\"scope\":\"*\",\"username\":\"YOUR_LOGIN\",\"password\":\"YOUR_PASSWORD\"}","op1type":"nul","op2type":"json","duration":"60","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":390,"y":70,"wires":[["f10564e7614330b8","c742ab5ee2d53ef1"]]},{"id":"f10564e7614330b8","type":"http request","z":"774bee5a7cc47aea","name":"token request","method":"POST","ret":"obj","paytoqs":"ignore","url":"http://YOUR_IP_ADDRESS:1880/auth/token","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[{"keyType":"Authorization","keyValue":"","valueType":"other","valueValue":"Bearer [token]"},{"keyType":"Content-Type","keyValue":"","valueType":"other","valueValue":"application/json"}],"x":410,"y":120,"wires":[["fcf506a8f3943a21","c742ab5ee2d53ef1"]]},{"id":"4bd0d6274ce3c870","type":"inject","z":"774bee5a7cc47aea","name":"trigger init","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"2","topic":"","payload":"true","payloadType":"bool","x":170,"y":120,"wires":[["672a9f73137e4e3b","c742ab5ee2d53ef1"]]},{"id":"c742ab5ee2d53ef1","type":"debug","z":"774bee5a7cc47aea","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":140,"wires":[]}]