Hello community, I´m working with REST API in order to communicate Node-RED with PLCnext Engineer. I wonder if you can help me with these questions: 1) After some tests I noticed that the Bearer token timeout is at most 20 min. Which is the timeout of the “Bearer token”? There is not information about it in the PLCnext infocenter. 2) Can be modified the “Bearer token” timeout? Where? 3) About session creation: how should be the content of the body in the client request? I tried In Node-RED in JSON format, but the results were “Invalid Request”. I attached a picture about this configuration. 4) Is it necessary always to include the “Bearer token” in the header requests? According to some examples, the “Bearer token” sometimes is in the header. However, I was able to create a session without the “Bearer token” in the header with other REST tools. 5) According to information from the PLCnext infocenter, the „Maintain Session“ command keeps a session active in order to avoid a session timeout. But in my experience, it does not keep the session active. With “Report sessions” command I verified that the session did not remain active once the configured timeout was reached. What is the correct behavior according REST? 6) From question 1 and 2, if the PLC is restarted, the REST connection is down (I suspect that we have to generate a new “Bearer Token”). Is there a way to generate a new bearer token automatically? I hope that you can help me with this topic, I think that REST API could be a great way to communicate the GDS with external apps using this technology ? Thanks in advanced, have a nice weekend!
I will attach an example flow that works well for me. Please have a look at it and see if that works. When the user authentication is disabled, the REST API can be used without authentication. [{"id":"226f24ea.618d8c","type":"tab","label":"REST","disabled":false,"info":"REST API mit Authentifizierung"},{"id":"96b3a202.6a5","type":"inject","z":"226f24ea.618d8c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["f002c010.9c3f3"]]},{"id":"f002c010.9c3f3","type":"exec","z":"226f24ea.618d8c","command":"curl -k --data '{\"scope\":\"variables\"}' https://192.168.178.10/_pxc_api/v1.2/auth/auth-token","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"auth token","x":310,"y":80,"wires":[["bd9f5128.0d51f"],[],[]]},{"id":"b0036f51.626c3","type":"debug","z":"226f24ea.618d8c","name":"auth token","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":60,"wires":[]},{"id":"bd9f5128.0d51f","type":"json","z":"226f24ea.618d8c","name":"","property":"payload","action":"","pretty":false,"x":510,"y":60,"wires":[["4ab8377.8c8cac8"]]},{"id":"4ab8377.8c8cac8","type":"function","z":"226f24ea.618d8c","name":"auth token","func":"flow.set('auth', msg.payload.code);\ msg.payload = msg.payload.code;\ return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":670,"y":60,"wires":[["b0036f51.626c3","d5da2a5.e7c6bd8"]]},{"id":"d5da2a5.e7c6bd8","type":"function","z":"226f24ea.618d8c","name":"","func":"var tok = flow.get('auth');\ \ var dat =\ {\"code\": tok, \ \"grant_type\": \"authorization_code\", \ \"username\": \"admin\", \ \"password\": \"a51e4524\"};\ msg.payload = dat;\ return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":200,"wires":[["a7310e0a.87848"]]},{"id":"f617629c.416d6","type":"exec","z":"226f24ea.618d8c","command":"","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"access token","x":770,"y":200,"wires":[["1e2d6c79.292194"],[],[]]},{"id":"bb9b8da8.7783c","type":"debug","z":"226f24ea.618d8c","name":"access token","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":180,"wires":[]},{"id":"1e2d6c79.292194","type":"json","z":"226f24ea.618d8c","name":"","property":"payload","action":"","pretty":false,"x":910,"y":180,"wires":[["65fbf581.34911c"]]},{"id":"65fbf581.34911c","type":"function","z":"226f24ea.618d8c","name":"access token","func":"flow.set('access',msg.payload.access_token);\ msg.payload = msg.payload.access_token;\ return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1050,"y":180,"wires":[["bb9b8da8.7783c","1c08de5b.85f1a2"]]},{"id":"a7310e0a.87848","type":"json","z":"226f24ea.618d8c","name":"","property":"payload","action":"","pretty":false,"x":470,"y":200,"wires":[["ceea87fa.b587f8"]]},{"id":"ceea87fa.b587f8","type":"function","z":"226f24ea.618d8c","name":"","func":"var mes = 'curl -k https://192.168.178.10/_pxc_api/v1.2/auth/access-token --data ';\ \ msg.payload = mes + \"'\"+msg.payload+\"'\";\ return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":200,"wires":[["f617629c.416d6"]]},{"id":"fad60635.092788","type":"comment","z":"226f24ea.618d8c","name":"1. request authentication token","info":"","x":170,"y":20,"wires":[]},{"id":"525e6dd.4ff9d94","type":"comment","z":"226f24ea.618d8c","name":"2. request access token","info":"","x":140,"y":160,"wires":[]},{"id":"d3252842.1b38e8","type":"http request","z":"226f24ea.618d8c","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"9bd7fd09.bf60e","persist":false,"proxy":"","authType":"","x":690,"y":300,"wires":[["d3d7b61d.39f8f8"]]},{"id":"d3d7b61d.39f8f8","type":"json","z":"226f24ea.618d8c","name":"","property":"payload","action":"","pretty":false,"x":850,"y":300,"wires":[["47a812e0.ece794"]]},{"id":"1c08de5b.85f1a2","type":"function","z":"226f24ea.618d8c","name":"http://192.168.178.10:1880/#func-tab-init","func":"var acc = flow.get('access');\ msg.headers = {};\ msg.headers['Authorization']= acc;\ msg.url = \"192.168.178.10/_pxc_api/variables?paths=Arp.Plc.Eclr/RESTAPI1.RESTAPI_REAL1\"\ \ return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":300,"wires":[["d3252842.1b38e8"]]},{"id":"568698d0.c7ac38","type":"comment","z":"226f24ea.618d8c","name":"3. get variable value from PLC","info":"","x":140,"y":260,"wires":[]},{"id":"47a812e0.ece794","type":"change","z":"226f24ea.618d8c","name":"display 2 decimals","rules":[{"t":"set","p":"payload.variables[0].value","pt":"msg","to":"$round(payload.variables[0].value, 2)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":300,"wires":[["55153aa.7f950c4"]]},{"id":"55153aa.7f950c4","type":"debug","z":"226f24ea.618d8c","name":"variable value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.variables[0].value","targetType":"msg","statusVal":"","statusType":"auto","x":1220,"y":300,"wires":[]},{"id":"9bd7fd09.bf60e","type":"tls-config","name":"","cert":"","key":"","ca":"","certname":"","keyname":"","caname":"","servername":"","verifyservercert":false}]
Hi Valentin, I’ll try to give you some answers: 1 + 2) The timeout seems to be indeed 20 min, but I’ll check this back with the dev. Afterwards we’ll update the documentation. 3) Please check the postman collectionfor the details how the calls should be done. 4) The key/pair {„Authorization“: „Bearer xyz“} must be part of the header for any data calls 5) A quote from the info center [quote]If desired, the web server can track the set of active client stations based on their corresponding session information. Otherwise, the web server automatically considers each station to be anonymous.[/quote] 6) The bearer is random generator, you can not generate a bearer by yourself Take care, Frank
Hi Valentin, I’ve finally received an answer for the timeout of the bearer. The timeout is bound to the general timeout of the usermanager, which is in default 20 minutes. As this is a global setting and part of the general device configs we do not recommend to change those settings. Furtheron will this settings be overwritten with every further firmware update. Take care, Frank