EMQ X 的 HTTP API 使用 Basic 认证 (opens new window) 方式,EMQ X 的 HTTP API 使用 Basic 认证 (opens new window) 方式,id
和 password
须分别填写 AppID 和 AppSecret。 默认的 AppID 和 AppSecret 是:admin/public
。你可以在 Dashboard 的左侧菜单栏里,选择 "管理" -> "应用" 来修改和添加 AppID/AppSecret。
在HTTP事务的上下文中,基本访问身份验证是HTTP 用户代理(例如Web 浏览器)在发出请求时提供用户名和密码的一种方法。在基本的 HTTP 身份验证中,请求包含形式为 的标头字段Authorization: Basic <credentials>
,其中凭据是ID 和密码的Base64编码,由单个冒号连接:
。
它在2015年的RFC 7617 中指定,它从 1999 年起废弃了RFC 2617
上述这段话最关键的是“其中凭据是ID 和密码的Base64编码,由单个冒号连接:”。在进行Base64编码前数据应该按如下形式准备:admin:public
EMQ 详细说明
表明试验成功。但不能止步于此,需进一步探究GET请求发送了怎样的数据。还是利用POSTMAN。
Authorization: Basic <credentials>格式对应的具体数据,现在
“YWRtaW46cHVibGlj”就是admin:public经过Base64编码后的结果。
点击Base64链接
进入后把Sample Flow的代码导入到NodeRed中,
得到如下流图:
部署后运行得到结果:
具体源码:
[ { "id": "688fc49188120316", "type": "tab", "label": "流程 5", "disabled": false, "info": "", "env": [] }, { "id": "d2ccae00.2d335", "type": "inject", "z": "688fc49188120316", "name": "", "props": [ { "p": "payload", "v": "", "vt": "str" }, { "p": "topic", "v": "", "vt": "str" } ], "repeat": "", "crontab": "", "once": false, "topic": "", "payload": "", "payloadType": "str", "x": 230, "y": 360, "wires": [ [ "e03cae10.1fc35" ] ] }, { "id": "b778ef09.48871", "type": "base64", "z": "688fc49188120316", "name": "", "action": "", "property": "payload", "x": 525.5, "y": 361, "wires": [ [ "6295d1b1.9d6a3", "46b597ba.b94a68" ] ] }, { "id": "6295d1b1.9d6a3", "type": "debug", "z": "688fc49188120316", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 724, "y": 361, "wires": [] }, { "id": "ead9e7c9.152618", "type": "debug", "z": "688fc49188120316", "name": "", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 724, "y": 441, "wires": [] }, { "id": "46b597ba.b94a68", "type": "base64", "z": "688fc49188120316", "name": "", "action": "", "property": "payload", "x": 525.5, "y": 441, "wires": [ [ "ead9e7c9.152618" ] ] }, { "id": "1c9124e9.e36edb", "type": "inject", "z": "688fc49188120316", "name": "", "repeat": "", "crontab": "", "once": false, "topic": "", "payload": "", "payloadType": "date", "x": 1889, "y": 314, "wires": [ [] ] }, { "id": "48a892ea.b7576c", "type": "debug", "z": "688fc49188120316", "name": "", "active": true, "console": "false", "complete": "false", "x": 2285, "y": 411, "wires": [] }, { "id": "e03cae10.1fc35", "type": "function", "z": "688fc49188120316", "name": "", "func": "msg.payload = new Buffer.from(\"admin:public\");\nreturn msg;", "outputs": 1, "noerr": 0, "initialize": "", "finalize": "", "libs": [], "x": 364, "y": 361, "wires": [ [ "b778ef09.48871" ] ] } ]