Http request Bearer Token

Hallo Leute,

Ich bin neu hier und hoffe es ist nicht schlimm wenn ich auf Deutsch schreibe, weil meine Englisch Kenntnisse nicht die besten sind oder mann es dann nicht verstehen könnte.

Ich habe folgendes Problem, ich greife Daten von einem Anderen Server ab, der mir nicht gehört, aber die Rechte habe dadrauf zuzugreifen. Die Daten bekommen ich nur über http Request und der Authentifizierungsmethode Bearer Token, diesen bekomme ich auch alle 15 min neu zurück. Jetzt die frage, wie kann ich den aktualisierten Token automatisch in die http request Node bekommen? Für eine Antwort wäre ich sehr dankbar. Ich hoffe man kann mein Problem etwas verstehen.

--------- Englisch -------

Hi Guys,

I am new here and I hope it is not bad if I write in German because my knowledge of English is not the best or one could not understand it.

I have the following problem, I am accessing data from another server that I do not own, but I have the rights to access it. I only get the data via http request and the authentication method Bearer Token, which I also get back every 15 minutes. Now the question is, how can I get the updated token automatically into the http request node? I would really appreciate an answer. I would really appreciate an answer. I hope you can understand my problem somewhat.

Hallo und willkommen hier!

Soweit ich das verstanden habe, wird das Token im HTTP-Header mitgegeben. Den kannst du dem HTTP Request Node in der Message mitgeben.

Das Token abc123 könntest du also prinzipell der Anfrage mitgeben, in dem du z.B. in einem Funktionsknoten das Header-Objekt setzt.

msg.header = { 'Authorization': 'Bearer abc123' };

Hallo kuema, vielen dank für deine schnelle Antwort. Leider sind meine Kenntnisse noch etwas dürftig in diesem Bereich, deswegen könnte ich etwas Hilfe gebrauch wie mann welche nodes mit leben füllt. Momentan mache ich es so, das ich in der Request Node den Token manuell von der Website einfüge, das sollte dann automatische gehen. Habe zum besseren Verständnis den Flow mal mit gepostet.

Vielen lieben Dank im vorraus

[{"id":"c6b51061.867b8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"3b5dc31.f13743c","type":"inject","z":"c6b51061.867b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":220,"wires":[["2255cfcf.886c6"]]},{"id":"2255cfcf.886c6","type":"http request","z":"c6b51061.867b8","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"https://s-o-b.eu/home/","tls":"","persist":false,"proxy":"","authType":"bearer","x":460,"y":220,"wires":[["e74a6b0e.5da5d8","d773ff4d.72d63"]]},{"id":"423decc8.79a194","type":"change","z":"c6b51061.867b8","name":"","rules":[{"t":"set","p":"token","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":220,"wires":[["39c31d3a.2fcdc2"]]},{"id":"e74a6b0e.5da5d8","type":"function","z":"c6b51061.867b8","name":"Token","func":"msg.payload = msg.responseCookies.sob_token.value; \nreturn msg;","outputs":1,"noerr":0,"x":630,"y":220,"wires":[["423decc8.79a194","7b6b7f50.c6c2c"]]},{"id":"39c31d3a.2fcdc2","type":"debug","z":"c6b51061.867b8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":980,"y":220,"wires":[]},{"id":"d773ff4d.72d63","type":"debug","z":"c6b51061.867b8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","x":650,"y":160,"wires":[]},{"id":"7b6b7f50.c6c2c","type":"debug","z":"c6b51061.867b8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":890,"y":160,"wires":[]}]

Zum Verständnis:

  • Der (neue) Token wird in der Antwort als Cookie mitgesendet?
  • Dieser ändert sich automatisch alle 15min, hat also auch nur eine begrenzte Gültigkeit?
    In diesem Fall müsste auch regelmäßig eine Anfrage gestellt werden, da sonst kein gültiger Token mehr vorliegt.

Ich würde an die Sache so herangehen: Den Token nach dem Request merkt man sich in einer Flow- oder Globalvariable, und gibt dieses dann in jeder neuen Anfrage wieder in msg.header.

Hier ein grobes Beispiel:

[{"id":"acc4c177.aeb45","type":"inject","z":"9d626f16.d345a8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":220,"wires":[["4a456cd5.2f2afc"]]},{"id":"aa912351.fbfc8","type":"http request","z":"9d626f16.d345a8","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://s-o-b.eu/home/","tls":"","persist":false,"proxy":"","authType":"bearer","x":530,"y":220,"wires":[["c4dbaeab.2a5598","8e76f529.e3c24"]]},{"id":"36393bb1.09088c","type":"change","z":"9d626f16.d345a8","name":"","rules":[{"t":"set","p":"token","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":900,"y":220,"wires":[["b6642d24.e7228"]]},{"id":"c4dbaeab.2a5598","type":"function","z":"9d626f16.d345a8","name":"Token","func":"let token = msg.responseCookies.sob_token.value; \n\nflow.set('token', token);\n\nmsg.payload = token; \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":220,"wires":[["36393bb1.09088c","e8b2f664.65abc"]]},{"id":"b6642d24.e7228","type":"debug","z":"9d626f16.d345a8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1180,"y":220,"wires":[]},{"id":"8e76f529.e3c24","type":"debug","z":"9d626f16.d345a8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","x":710,"y":160,"wires":[]},{"id":"e8b2f664.65abc","type":"debug","z":"9d626f16.d345a8","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","x":1000,"y":160,"wires":[]},{"id":"4a456cd5.2f2afc","type":"function","z":"9d626f16.d345a8","name":"Token -> Header","func":"let token = flow.get('token');\n\nmsg.header = {\n    'Authorization': 'Bearer ' + token\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":220,"wires":[["aa912351.fbfc8","8608768d.b91838"]]},{"id":"80fdb59c.dde44","type":"inject","z":"9d626f16.d345a8","name":"setze Token","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"meintoken","payloadType":"str","x":170,"y":140,"wires":[["7f28d489.b1a094"]]},{"id":"7f28d489.b1a094","type":"change","z":"9d626f16.d345a8","name":"","rules":[{"t":"set","p":"token","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":140,"wires":[[]]},{"id":"8608768d.b91838","type":"debug","z":"9d626f16.d345a8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":300,"wires":[]}]

Der Token für die Erstanfrage muss mit dem Inject-node "setze Token" manuell gesetzt werden.

Der (neue) Token wird in der Antwort als Cookie mitgesendet?

JA!

Dieser ändert sich automatisch alle 15min, hat also auch nur eine begrenzte Gültigkeit?
In diesem Fall müsste auch regelmäßig eine Anfrage gestellt werden, da sonst kein gültiger Token mehr vorliegt.

Nein Das Token ändert sich alle 7 Tage, aber dadurch das ich alle 15 Min die Daten von der Website abfrage bekomme ich alle 15 Min einen neuen Token mitgeliefert als "responseCookies",

Dank deiner Anleitung konnte ich es aber jetzt in mein Projekt mit einbinden und es funktioniert sehr gut. Vielen Dank für deine Arbeit Liebe Grüße aus der Mitte Deutschlands :wink:

1 Like

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