HTTP Request with dynamic authorization token

Hi all,
I am struggling trying to build a flow to interact with a Teltonika router API.
To use it I have to make a first http request to receive the authorization token and then a second one (the actual request to the API) using the received token.
Using the HTTP Request node with Bearer authentication and manually copying the token into the token field everything works flawlessly.
Now the issue is that for every request I need to ask for a new token.
I have not been able to design a flow that injects dynamically the new token into the HTTP Request node.
I have tried setting the headers property in a preceding node for authorization and setting the authorization header inside the HTTP Request node to no avail unfortunately.
Has anyone successfully managed to do something similar?
Thank you in advance

Hi .. i have used the Teltonika routers .. i was playing around with sms api on some cellular model
the flow below can help you with passing login / token from first request to second ..

[{"id":"9b7633d79e714bd3","type":"inject","z":"54efb553244c241f","name":"credentials","props":[{"p":"payload"},{"p":"rejectUnauthorized","v":"false","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"username\":\"admin\",\"password\":\"xxxxxx\"}","payloadType":"json","x":280,"y":3820,"wires":[["8587587c463c44da"]]},{"id":"8587587c463c44da","type":"http request","z":"54efb553244c241f","name":"login","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://192.168.1.1/api/login","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":430,"y":3820,"wires":[["38833889427d991b"]]},{"id":"b51c56d6e84f3ee5","type":"debug","z":"54efb553244c241f","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":3820,"wires":[]},{"id":"8a304f2cecf1937e","type":"http request","z":"54efb553244c241f","name":"SMS","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://192.168.1.1/api/messages/actions/send","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":750,"y":3820,"wires":[["b51c56d6e84f3ee5"]]},{"id":"38833889427d991b","type":"function","z":"54efb553244c241f","name":"message","func":"if (msg.statusCode === 200) {\n\n    msg.headers = {\n        \"Authorization\": `Bearer ${msg.payload.data.token}`,\n        \"Content-Type\": \"application/json\"\n    }\n    msg.rejectUnauthorized = false\n    msg.payload = {\n        \"data\": {\n            \"number\": \"1234567890\",\n            \"message\": \"SMS Test from Teltonika and Node-red\",\n            \"modem\": \"3-1\"\n        }\n    }\n    return msg;\n}\n\nelse {\n    return null\n}\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":3820,"wires":[["e072d01fd09e2ae2","8a304f2cecf1937e"]]},{"id":"e072d01fd09e2ae2","type":"debug","z":"54efb553244c241f","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":3740,"wires":[]}]

Thank you so much UnborN, I have tried your flow but it seems to not be working and I receive the following error:

[{"source":"Authorization","code":120,"error":"Unauthorized"}]

Here is an example for handling the token...


You need this node node-red-contrib-credentials (node) - Node-RED for storing the credentials (or hardcode them into your flow, which i don't recommend)

I have the RUT200 with FW version RUT2M_R_00.07.14.3

[{"id":"35b76acf05c36d89","type":"http request","z":"8c6cb615b3919618","name":"login","method":"POST","ret":"obj","paytoqs":"ignore","url":"http://10.0.0.0/api/login","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":990,"y":1040,"wires":[["66a59d34cec9f067"]]},{"id":"1aeed8322105ce23","type":"link in","z":"8c6cb615b3919618","name":"get token","links":[],"x":65,"y":1080,"wires":[["0c8c048f5f617178"]]},{"id":"1dcbfb301a0de42c","type":"link out","z":"8c6cb615b3919618","name":"link out 19","mode":"return","links":[],"x":1695,"y":1080,"wires":[]},{"id":"66a59d34cec9f067","type":"change","z":"8c6cb615b3919618","name":"","rules":[{"t":"set","p":"payload.data.ts","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":1210,"y":1040,"wires":[["81d240e0802e3554"]]},{"id":"81d240e0802e3554","type":"change","z":"8c6cb615b3919618","name":"","rules":[{"t":"set","p":"token","pt":"flow","to":"payload","tot":"msg","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":1080,"wires":[["8930710d38b22550"]]},{"id":"0c8c048f5f617178","type":"switch","z":"8c6cb615b3919618","name":"has ts?","property":"token.data","propertyType":"flow","rules":[{"t":"hask","v":"ts","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":1080,"wires":[["0bf608e182a65ac2"],["8bbf9fa58985419a"]]},{"id":"3b057a036f57f33b","type":"credentials","z":"8c6cb615b3919618","name":"","props":[{"value":"username","type":"msg"},{"value":"password","type":"msg"}],"x":650,"y":1040,"wires":[["5092534c4d41c881"]]},{"id":"5092534c4d41c881","type":"function","z":"8c6cb615b3919618","name":"function 13","func":"msg.payload = {\n    username: msg.username,\n    password: msg.password\n}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":1040,"wires":[["35b76acf05c36d89"]]},{"id":"0bf608e182a65ac2","type":"function","z":"8c6cb615b3919618","name":"function 14","func":"const now = new Date()\nconst token  = flow.get(\"token\");\nconst token_created = token.data.ts\nconst token_expire_at = token_created + token.data.expires * 1000\n\nif (now > token_expire_at) {\n    node.status({fill:\"yellow\",shape:\"dot\",text:\"token expired\"});\n    return [null,msg];\n\n} else {\n    node.status({fill:\"green\",shape:\"dot\",text:\"tolen valid\"});\n    return [msg, null];\n}\n\n\n","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":980,"wires":[["bf9f3fbc5ef0fd22"],["3b057a036f57f33b"]]},{"id":"b753a86a1c32ad9e","type":"change","z":"8c6cb615b3919618","name":"","rules":[{"t":"set","p":"token","pt":"msg","to":"token.data.token","tot":"flow","dc":true},{"t":"set","p":"token","pt":"msg","to":"\"Bearer \" & token ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":1080,"wires":[["1dcbfb301a0de42c"]]},{"id":"18b2615f70f1be56","type":"link call","z":"8c6cb615b3919618","name":"","links":["1aeed8322105ce23"],"linkType":"static","timeout":"30","x":300,"y":1220,"wires":[["8361aa94c1bb71a7"]]},{"id":"ff9fa84d5341faab","type":"inject","z":"8c6cb615b3919618","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":1220,"wires":[["18b2615f70f1be56"]]},{"id":"53c74677512b798b","type":"debug","z":"8c6cb615b3919618","name":"debug 51","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":720,"y":1220,"wires":[]},{"id":"8361aa94c1bb71a7","type":"http request","z":"8c6cb615b3919618","name":"system/device/status","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://10.0.0.0/api/system/device/status","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Authorization","keyValue":"","valueType":"msg","valueValue":"token"}],"x":500,"y":1220,"wires":[["53c74677512b798b"]]},{"id":"207a5335a3984e4e","type":"comment","z":"8c6cb615b3919618","name":"change ip here","info":"","x":980,"y":1080,"wires":[]},{"id":"d252d3673be15d47","type":"comment","z":"8c6cb615b3919618","name":"change ip here","info":"","x":500,"y":1260,"wires":[]},{"id":"8930710d38b22550","type":"junction","z":"8c6cb615b3919618","x":1440,"y":1080,"wires":[["b753a86a1c32ad9e"]]},{"id":"bf9f3fbc5ef0fd22","type":"junction","z":"8c6cb615b3919618","x":1380,"y":980,"wires":[["8930710d38b22550"]]},{"id":"8bbf9fa58985419a","type":"junction","z":"8c6cb615b3919618","x":520,"y":1100,"wires":[["3b057a036f57f33b"]]}]

Hi UnborN, I don't know what I have made wrong before, but starting from scratch with your flow, finally it worked fine.
Thanks

Hi Kitori, thank you so much for sharing your flow.
I have anyway used UnborN one because with less nodes it achieves what I needed.