Sending the TOKEN seperate of the block

Hi,

I'm reading out some data with API, now I want to read different values but that will say that I need different tokens. I would ask how I can send the token I want to use. So if I didn't fill anything in the part
'Token', is there is possibility to fill it in with an extern block like inject?

Have you tried sending the authorization in the headers

Authorization: Bearer {token}

@E1cid

Dp you mean to set this in the timestamp? If I try this it didn't work

image

No set a msg.headers object prior to the http request.
{"Authorization": "Bearer " + token}
You would need to base64 encode the token if it is not already.

[edit]
example of setting msg.headers.Authorization using a change node

[{"id":"f9316e19.7381e","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"token","v":"sdfgh345666","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":120,"y":1160,"wires":[["7387c36c.54496c"]]},{"id":"7387c36c.54496c","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $base64encode($$.token)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":1140,"wires":[["7943a6c5.b78e4"]]},{"id":"7943a6c5.b78e4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":1160,"wires":[]}]

No, @E1cid means have you set the incoming msg.headers to an object containing Authorization: Bearer {token} (where token is your token)

e.g.

  • turn off "Use Authentication"
  • in a change node or function node (before the HTTP Request)
    • msg.headers = {Authorization: "Bearer " + msg.token}};

(where msg.token is a base64 encoded token)

I don't understand how I can do this, which nodes does I need?

Did you try the example @E1cid kindly provided?

[{"id":"132a311fdb8354c4","type":"http request","z":"c3c9206917830458","name":"Switch sturen [1]","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"","senderr":true,"x":460,"y":340,"wires":[["0568392c58ab7aa4"]]},{"id":"0568392c58ab7aa4","type":"debug","z":"c3c9206917830458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":340,"wires":[]},{"id":"9cd6ac5162824796","type":"inject","z":"c3c9206917830458","name":"Switch ON","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":180,"y":340,"wires":[["132a311fdb8354c4"]]},{"id":"3cca0cbf31139cd5","type":"http request","z":"c3c9206917830458","name":"Switch sturen [0]","method":"POST","ret":"obj","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/off","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":460,"y":380,"wires":[["5d11ae9ab9d89b4f"]]},{"id":"5d11ae9ab9d89b4f","type":"debug","z":"c3c9206917830458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":770,"y":380,"wires":[]},{"id":"7f723fe72df17ab1","type":"inject","z":"c3c9206917830458","name":"Switch OFF","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":170,"y":380,"wires":[["3cca0cbf31139cd5"]]},{"id":"72db6a0f32dffb78","type":"comment","z":"c3c9206917830458","name":"info switch on/off","info":"switch on/off, hiermee kunnen we de switch gaan aan- en uitsturen. We sturen zelf de duration mee, daarnaast moeten we nog de on/off gaan meesturen indien we het willen vanuit één blok doen","x":460,"y":300,"wires":[]},{"id":"f9316e19.7381e","type":"inject","z":"c3c9206917830458","name":"","props":[{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":130,"y":500,"wires":[["7387c36c.54496c"]]},{"id":"7387c36c.54496c","type":"change","z":"c3c9206917830458","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $base64encode($$.token)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":480,"wires":[["132a311fdb8354c4","3cca0cbf31139cd5"]]}]

Yes I tried this, but it didn't work
image

Appears your authorization is already encodeded
this return no errors for me

[{"id":"47742e80.4d9b5","type":"inject","z":"bf9e1e33.030598","name":"Switch ON","props":[{"p":"payload"},{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":100,"y":1720,"wires":[["5e386841.b5165"]]},{"id":"5e386841.b5165","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $$.token","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":1740,"wires":[["ade26476.383b"]]},{"id":"ade26476.383b","type":"http request","z":"bf9e1e33.030598","name":"Switch sturen [1]","method":"POST","ret":"txt","paytoqs":"body","url":"https://app1pub.smappee.net/dev/v3/servicelocation/59792/actuator/1/on","tls":"","persist":false,"proxy":"","authType":"","x":420,"y":1800,"wires":[["baf21866.52c91"]]},{"id":"ccbdbaea.cbce8","type":"inject","z":"bf9e1e33.030598","name":"Switch OFF","props":[{"p":"payload"},{"p":"token","v":"a733c03b-a21e-3d95-94f1-ecf782696280","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"duration\": 300}","payloadType":"json","x":140,"y":1780,"wires":[["5e386841.b5165"]]},{"id":"baf21866.52c91","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":1720,"wires":[]}]

Yes, indeed it works!

But the next question, is it possible to set different tokens? maybe with a variable.

So what I mean is this:
If I have a global variable, I can change it whenever I want with a text field. But how can I set the global variable in the inject node?

That would be just token, but why pay the middleman, when you can set the global in the change node

[{"id":"815dc892.8a7f08","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"headers.Authorization","pt":"msg","to":"\"Bearer \" & $globalContext(\"token\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":1980,"wires":[["8fcd946a.13cff8"]]}]
"Bearer " & $globalContext("token")

Time to mark a solution to help others find answers, If you have new questions start a new topic.

If I wtry to use the UserMatrix[6][1] it doesn't work..

Look again at the example @E1cid provided. You have the JSONata syntax wrong.

How do you mean?

Can't you see where the value you want to use would go if you followed the provided example ...

E.g...

"Bearer " & $globalContext("some-other-named-global[1][2]")

In fact, @E1cid gave you the answer here: URL outside of the http request - #2 by E1cid

Please dont cross post - it just gets messy.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.