oh…
That’s an old one, I haven’t used it in a long while.
Try this one:
[
{
"id": "cf0290052fc9e037",
"type": "subflow",
"name": "farmOS Oauth2V2",
"info": "",
"category": "farmOS V2",
"in": [
{
"x": 60,
"y": 260,
"wires": [
{
"id": "acfc46e21bdf62c6"
}
]
}
],
"out": [
{
"x": 1230,
"y": 260,
"wires": [
{
"id": "3d0bb775fbb6d738",
"port": 0
}
]
},
{
"x": 950,
"y": 120,
"wires": [
{
"id": "9eb02a196b5e91bd",
"port": 0
}
]
}
],
"env": [
{
"name": "grant",
"type": "str",
"value": "client",
"ui": {
"icon": "font-awesome/fa-id-card-o",
"label": {
"en-US": "Grant Type"
},
"type": "select",
"opts": {
"opts": [
{
"l": {
"en-US": "authorization code"
},
"v": "code"
},
{
"l": {
"en-US": "client credentials"
},
"v": "client"
},
{
"l": {
"en-US": "password"
},
"v": "password"
}
]
}
}
},
{
"name": "scope",
"type": "str",
"value": "farm_worker",
"ui": {
"type": "select",
"opts": {
"opts": [
{
"l": {
"en-US": "Manager"
},
"v": "farm_manager"
},
{
"l": {
"en-US": "Viewer"
},
"v": "farm_viewer"
},
{
"l": {
"en-US": "Worker"
},
"v": "farm_worker"
}
]
}
}
},
{
"name": "url",
"type": "str",
"value": ""
},
{
"name": "redirect",
"type": "str",
"value": ""
},
{
"name": "client",
"type": "cred"
},
{
"name": "secret",
"type": "cred"
},
{
"name": "username",
"type": "cred"
},
{
"name": "password",
"type": "cred"
}
],
"meta": {},
"color": "#C7E9C0",
"outputLabels": [
"Debug",
"Autherization Code URL"
],
"icon": "node-red-contrib-oauth2/oauth2.svg"
},
{
"id": "ce468c0ef9940f5f",
"type": "function",
"z": "cf0290052fc9e037",
"name": "authorization_code",
"func": "var farmOSurl = env.get('url');\nglobal.set('farmOSurl', farmOSurl);\n\nvar client = env.get('client');\nvar redirect = env.get('redirect');\nvar secret = env.get('secret');\nvar scope = env.get('scope');\n\nmsg.payload ={\n \"grant_type\": \"authorization_code\",\n \"client_id\": client,\n \"code\": msg.payload.code,\n \"state\": msg.payload.state,\n \"redirect_uri\": redirect,\n \"client_secret\": secret,\n \"scope\": scope\n }\n\nmsg.headers = {};\nmsg.headers['Accept'] = \"application/json\";\nmsg.headers['content-type'] = 'application/x-www-form-urlencoded';\n\nmsg.method = \"POST\";\nmsg.url = env.get('url') + \"/oauth/token\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 180,
"wires": [
[
"9210259c7f2b215c"
]
]
},
{
"id": "9210259c7f2b215c",
"type": "http request",
"z": "cf0290052fc9e037",
"name": "",
"method": "use",
"ret": "obj",
"paytoqs": "ignore",
"url": "",
"tls": "bec8412e2fdbd417",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 650,
"y": 260,
"wires": [
[
"4b6637a23189e0ac"
]
]
},
{
"id": "33606295cfc54171",
"type": "function",
"z": "cf0290052fc9e037",
"name": "client_credentials",
"func": "var farmOSurl = env.get('url');\nglobal.set('farmOSurl', farmOSurl);\n\nvar client = env.get('client');\nvar secret = env.get('secret');\nvar scope = env.get('scope');\n\nmsg.payload = {\n \"grant_type\": \"client_credentials\",\n \"client_id\": client,\n \"client_secret\": secret,\n \"scope\": scope\n}\n\nmsg.headers = {};\nmsg.headers['Accept'] = \"application/json\";\nmsg.headers['content-type'] = 'application/x-www-form-urlencoded';\n\nmsg.method = \"POST\";\nmsg.url = env.get('url') + \"/oauth/token\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 390,
"y": 260,
"wires": [
[
"9210259c7f2b215c"
]
]
},
{
"id": "acfc46e21bdf62c6",
"type": "switch",
"z": "cf0290052fc9e037",
"name": "",
"property": "grant",
"propertyType": "env",
"rules": [
{
"t": "eq",
"v": "code",
"vt": "str"
},
{
"t": "eq",
"v": "client",
"vt": "str"
},
{
"t": "eq",
"v": "password",
"vt": "str"
}
],
"checkall": "false",
"repair": true,
"outputs": 3,
"x": 190,
"y": 260,
"wires": [
[
"5d16f61bb9acef3e"
],
[
"33606295cfc54171"
],
[
"3c15790622ac845b"
]
]
},
{
"id": "3c15790622ac845b",
"type": "function",
"z": "cf0290052fc9e037",
"name": "password",
"func": "var farmOSurl = env.get('url');\nglobal.set('farmOSurl', farmOSurl);\n\nvar client = env.get('client');\nvar scope = env.get('scope');\nvar username = env.get('username');\nvar password = env.get('password');\n\nmsg.payload = {\n \"grant_type\": \"password\",\n \"username\": username,\n \"password\": password,\n \"client_id\": client,\n \"scope\": scope\n}\n\nmsg.headers = {};\nmsg.headers['Accept'] = \"application/json\";\nmsg.headers['content-type'] = 'application/x-www-form-urlencoded';\n\nmsg.method = \"POST\";\nmsg.url = env.get('url') + \"/oauth/token\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 320,
"wires": [
[
"9210259c7f2b215c"
]
]
},
{
"id": "9eb02a196b5e91bd",
"type": "function",
"z": "cf0290052fc9e037",
"name": "Generate URL",
"func": "msg.payload = env.get(\"url\") + \"/oauth/authorize?response_type=code&client_id=\" + env.get(\"client\") + \"&scope=\" + env.get(\"scope\") + \"&redirect_uri=\" + env.get(\"redirect\") + \"&state=p4W8P5f7gJCIDbC1Mv78zHhlpJOidy\";\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 380,
"y": 120,
"wires": [
[]
]
},
{
"id": "5d16f61bb9acef3e",
"type": "switch",
"z": "cf0290052fc9e037",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "url",
"vt": "str"
},
{
"t": "hask",
"v": "code",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 190,
"y": 140,
"wires": [
[
"9eb02a196b5e91bd"
],
[
"ce468c0ef9940f5f"
]
]
},
{
"id": "4b6637a23189e0ac",
"type": "function",
"z": "cf0290052fc9e037",
"name": "Process tokens",
"func": "var time;\nif (msg.payload.access_token) {\n var token = msg.payload.access_token;\n var refresh = msg.payload.refresh_token;\n var expiry = Date.now() + (msg.payload.expires_in * 1000) - 120000;\n global.set('farmos_token_expiry', expiry);\n global.set('token', token);\n global.set('farmos_refresh', refresh);\n time = new Date();\n msg.bearer = msg.payload;\n msg.payload = \"Access Granted \" + time;\n} else {\n msg.error = msg.payload;\n msg.payload = msg.error.error;\n}\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 860,
"y": 260,
"wires": [
[
"3d0bb775fbb6d738"
]
]
},
{
"id": "3d0bb775fbb6d738",
"type": "change",
"z": "cf0290052fc9e037",
"name": "",
"rules": [
{
"t": "delete",
"p": "url",
"pt": "msg"
},
{
"t": "delete",
"p": "method",
"pt": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 1060,
"y": 260,
"wires": [
[]
]
},
{
"id": "bec8412e2fdbd417",
"type": "tls-config",
"z": "cf0290052fc9e037",
"name": "",
"cert": "",
"key": "",
"ca": "",
"certname": "",
"keyname": "",
"caname": "",
"servername": "",
"verifyservercert": false,
"alpnprotocol": ""
},
{
"id": "dc37d527c32af077",
"type": "inject",
"z": "0bd8a383a9bfc898",
"name": "",
"props": [
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 150,
"y": 200,
"wires": [
[
"c9a5e48f9887c6ab"
]
]
},
{
"id": "340cf27d9c9452f8",
"type": "debug",
"z": "0bd8a383a9bfc898",
"name": "debug 2",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 600,
"y": 200,
"wires": []
},
{
"id": "c9a5e48f9887c6ab",
"type": "subflow:cf0290052fc9e037",
"z": "0bd8a383a9bfc898",
"name": "",
"env": [
{
"name": "grant",
"value": "password",
"type": "str"
},
{
"name": "scope",
"value": "farm_manager",
"type": "str"
},
{
"name": "url",
"value": "https://your_farmOS_address",
"type": "str"
},
{
"name": "redirect",
"value": "https://ignore_unless_using_authorization_code",
"type": "str"
},
{
"name": "client",
"type": "cred"
},
{
"name": "secret",
"type": "cred"
},
{
"name": "username",
"type": "cred"
},
{
"name": "password",
"type": "cred"
}
],
"x": 370,
"y": 200,
"wires": [
[
"340cf27d9c9452f8"
],
[]
]
}
]