Using a Working curl with node-red http-request

I've read various posts here on the node-red way to provide data to an http-request node, but no matter what I try I am not getting anywhere fast.

(Because this contains sensitive data, I have had to obscure or modify certain parts.)

Running this:

curl -s "https://auth.xxx.com/oauth/token" -d client_id=xxx-web-app -d grant_type=password -d scope=home.user -d username="xxx@xxx.com" -d password="pet-syp-bom" -d client_secret="wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc"

Gets the expected:

{"access_token":"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1OTVlNDUxMDA5OGYzMzI1MGFkYzE2MzMiLCJ6b25laW5mbyI6IkV1cm9wZS9Mb25kb24iLCJ0YWRvX2hvbWVzIjpbeyJpZCI6MTg4MX1dLCJpc3MiOiJ0YWRvIiwibG9jYWxlIjoiZW4iLCJhdWQiOiJwYXJ0bmVyIiwibmJmIjoxNjU2NTc1NjAzLCJ0YWRvX3Njb3BlIjpbImhvbWUudXNlciJdLCJ0YWRvX3VzZXJuYW1lIjoiYmVybmFyZGhhcnRlQG1hYy5jb20iLCJuYW1lIjoiQmVybmFyZCBIYXJ0ZSIsImV4cCI6MTY1NjU3NjIwMywiaWF0IjoxNjU2NTc1NjAzLCJ0YWRvX2NsaWVudF9pZCI6InRhZG8td2ViLWFwcCIsImp0aSI6ImZmODBlYzMyLTRlMzgtNDVlNS05MjQ2LTFmNDY3ZWY1ZjJkZSIsImVtYWlsIjoiYmVybmFyZGhhcnRlQG1hYy5jb20ifQ.QYzV7vuCaso-3iV5or10ZRBuk44MmAYcAiy-8W7SAD8_Dkg6gvrSvK7m0I8lMdOF8kjCq8HaCs5h1J9IEeUHm2CHks86Vf3dzu8anOztk9S4J98RlUGrgND2PKsGNL69ZdILNASnRhImlz75isHYpGbond1e_PpI8ypQ9tG-WpvR0nBf7MrX64MwSLxDzUWMhSOXcQTHXE8WZFT3NcQiEOnp05QA_dyY57PLsDTyydE--IbFNsW36ZuC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w",
"expires_in":599,
"scope":"home.user",
"jti":"ff80ec32-4e38-45e5-9246-1f467ef5f2de"

When I try implementing (what I think is) the same in node-red [see below], I get an error: "invalid_grant" and error_description: "Bad credentials", so perhaps I am providing the username and password and/or the client_secret in the wrong place?

Any help would be much appreciated!

[
    {
        "id": "35bdb008ec1bf921",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5ca4e10a17fb8bd0",
        "type": "inject",
        "z": "35bdb008ec1bf921",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 200,
        "y": 120,
        "wires": [
            [
                "ce7a4b98b74cd230"
            ]
        ]
    },
    {
        "id": "ce7a4b98b74cd230",
        "type": "change",
        "z": "35bdb008ec1bf921",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "headers",
                "pt": "msg",
                "to": "{\"content-type\":\"application/x-www-form-urlencoded\"}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{\"client_id\":\"xxx-web-app\",\"grant_type\":\"password\",\"scope\":\"home.user\",\"username\":\"xxx@xxx.com\",\"password\":\"pet-syp-bom\",\"client_secret\":\"wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc\"}",
                "tot": "json"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 340,
        "y": 180,
        "wires": [
            [
                "814f9dbe9af659cf"
            ]
        ]
    },
    {
        "id": "814f9dbe9af659cf",
        "type": "http request",
        "z": "35bdb008ec1bf921",
        "name": "",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "query",
        "url": "https://auth.xxx.com/oauth/token",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 450,
        "y": 260,
        "wires": [
            [
                "7fe5d3240debc20a"
            ]
        ]
    },
    {
        "id": "7fe5d3240debc20a",
        "type": "debug",
        "z": "35bdb008ec1bf921",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 590,
        "y": 320,
        "wires": []
    }
]

Try this...

[{"id":"f8a108bcac1f01d2","type":"inject","z":"10ece3ab98e93683","name":"Click me","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":240,"y":920,"wires":[["880aa0ca5eab8a39"]]},{"id":"880aa0ca5eab8a39","type":"function","z":"10ece3ab98e93683","name":"","func":"//original curl:  \n//\n//curl -s \"https://auth.xxx.com/oauth/token\" -d client_id=xxx-web-app -d grant_type=password -d scope=home.user -d username=\"xxx@xxx.com\" -d password=\"pet-syp-bom\" -d client_secret=\"wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc\"\n\nmsg.method = \"post\";\nmsg.url = \"https://auth.xxx.com/oauth/token\";\nmsg.payload = `client_id=xxx-web-app&grant_type=password&scope=home.user&username=\"xxx@xxx.com\"&password=\"pet-syp-bom\"&client_secret=\"wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc\"`;\nmsg.headers = null;\nmsg.cookies = null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":920,"wires":[["9a2d79e221a4a227"]]},{"id":"9a2d79e221a4a227","type":"http request","z":"10ece3ab98e93683","name":"","method":"use","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"credentials":{"user":"","password":""},"x":600,"y":920,"wires":[["91953eeb95ea4395"]]},{"id":"91953eeb95ea4395","type":"debug","z":"10ece3ab98e93683","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":920,"wires":[]}]

It is an auto-generated flow from a plugin I am working on - so please let me know if it works.

Thank you, but I am afraid it doesn't work:

{"error":"unauthorized","error_description":"Full authentication is required to access this resource"}

(Obviously, I modified to correct credentials etc after importing.)

Can you modify the function node to this...

//original curl:  
//
//curl -s "https://auth.xxx.com/oauth/token" -d client_id=xxx-web-app -d grant_type=password -d scope=home.user -d username="xxx@xxx.com" -d password="pet-syp-bom" -d client_secret="wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc"

msg.method = "post";
msg.url = "https://auth.xxx.com/oauth/token";
msg.payload = `client_id=xxx-web-app&grant_type=password&scope=home.user&username="xxx@xxx.com"&password="pet-syp-bom"&client_secret="wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc"`;
msg.headers = null;
msg.cookies = {
    "Content-Type": "application/x-www-form-urlencoded"
};

return msg;

NOTE
be sure to update the following values (where required)

  • url
  • client_id
  • client_secret
  • username
  • password

Same result. (I have definitely updated all the values you list.)

If it's of any help, I am quite happy to share the actual details with you privately - I just don't want to publish them on the web.

1 Like

Please do as it would help me understand why my plugin doesnt translate your particular curl correctly.

1 Like

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