Executing curl in node red

Hi,

I am trying to execute a curl command in node red along the following lines:

curl 
      -H "Authorization: Bearer {{ state_attr('sensor.geo_accesstoken', 'accessToken') }}"
      -H "Accept: application/json" 
      -H "Content-Type: application/json"
      'https://api.geotogether.com/api/userapi/system/smets2-live-data/PUT_YOUR_SYSTEMID_HERE'

From my research I think I can do the following:

Get the Acccess token using an http request and creating a function node that sets a global variable as follows:

flow.set("Bearer_token", "Bearer " + msg.payload.accessToken);
return msg;

This seems to work ok.

Next I need to use the token in the next curl command above (see ‘accessToken’)

I am using another function node to set the message headers as indicated in the curl command above which I have as follows:

msg.headers = {"Authorisation" : flow.get("Bearer_token"),
                "Accept" : "application/json",
                "Content-Type" : "application/json"
};
return msg;

I am outputting this function node into a http node for the url in the curl statement above, but when a try to execute it is giving a 401 error “status”:401,“error”:“Unauthorized”,“message”:"No message

So it seems I am not providing the credentials in the right way.

Any assistance anyone?
Thanks

Here is the current version of the flows:

[{"id":"4681f3324412992f","type":"function","z":"6a33605ba7e01f9b","name":"","func":"\nmsg.payload = { \"identity\" : \"[email protected]\", \"password\": \"myverystrongpassword\" };\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":680,"wires":[["fcbb22fda0328a82"]]},{"id":"8bb2b9a66d545cf9","type":"inject","z":"6a33605ba7e01f9b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":680,"wires":[["4681f3324412992f"]]},{"id":"39328e20ef862967","type":"debug","z":"6a33605ba7e01f9b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1150,"y":680,"wires":[]},{"id":"fcbb22fda0328a82","type":"http request","z":"6a33605ba7e01f9b","name":"GEO Authentication","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.geotogether.com/usersservice/v2/login","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":620,"y":680,"wires":[["cbdd2da7999a355f"]]},{"id":"1929b55b23f4ed11","type":"function","z":"6a33605ba7e01f9b","name":"","func":"flow.set(\"Bearer_token\", \"Bearer \" + msg.payload.accessToken);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1000,"y":680,"wires":[["39328e20ef862967"]]},{"id":"cbdd2da7999a355f","type":"json","z":"6a33605ba7e01f9b","name":"","property":"payload","action":"","pretty":false,"x":830,"y":680,"wires":[["1929b55b23f4ed11"]]},{"id":"ba7d6e2a04a05e9d","type":"http request","z":"6a33605ba7e01f9b","name":"Live Data","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.geotogether.com/api/userapi/system/smets2-live-data/MYSYSTEMID","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{},"x":880,"y":820,"wires":[["5b2037945f17dd86"]]},{"id":"de13c8b61cfb4979","type":"inject","z":"6a33605ba7e01f9b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":270,"y":820,"wires":[["52673543d1cba814"]]},{"id":"52673543d1cba814","type":"function","z":"6a33605ba7e01f9b","name":"","func":"msg.headers = {\"Authorisation\" : flow.get(\"Bearer_token\"),\n                \"Accept\" : \"application/json\",\n                \"Content-Type\" : \"application/json\"\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":820,"wires":[["1e68e3a1583da98a"]]},{"id":"5b2037945f17dd86","type":"debug","z":"6a33605ba7e01f9b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":820,"wires":[]},{"id":"b32b4fbc756118b3","type":"debug","z":"6a33605ba7e01f9b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":900,"wires":[]},{"id":"1e68e3a1583da98a","type":"json","z":"6a33605ba7e01f9b","name":"","property":"payload","action":"","pretty":false,"x":710,"y":820,"wires":[["ba7d6e2a04a05e9d","b32b4fbc756118b3"]]}]

authorization spelled wrong in the request?

doh!!

ok some progress now. I get the following response from the debug node out of the http request:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
This distribution is not configured to allow the HTTP request method that was used for this request. The distribution supports only cachable requests.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: b5QN70b69OM_e28GRZ2y3wBQKL6asCY8LPlKcEB-6-rNC7...

I am not sure if the request type is the issue here. When you look at the curl command it doesn’t specify POST/GET etc.

The above response is what I get by setting the http request node METHOD to:

  • set by msg.method -

When I set it to GET the response says “unauthorised”

and when I set it to POST I get “Method not allowed”