使用 http-request 节点和承载身份验证连接到 REST API

Using http-request node and bearer authentication to connect to REST API

对于应用程序,我需要使用 Bearer 身份验证连接到 REST API。令牌有效期为 3 小时,因此我无法在 http-request 节点本身中静态填写它。

我已经尝试将其作为对象传递,但每次服务器返回 401 代码以及令牌无效的额外文本时。在 http-request 节点中配置令牌时,服务器 returns 使用相同令牌的 200 响应。

我正在使用连接到功能节点的注入节点对其进行测试,该功能节点连接到 http-request 节点。函数节点包含以下代码:

msg.headers = {};
msg.headers['Authorization'] = 'Bearer -mytoken-';

return msg;

有人可以解释一下为什么令牌在 http-request 节点中配置时有效,但在作为对象传递时却无效吗?为了接受不记名令牌作为对象,是否需要在 http-request 节点中进行任何设置?

尝试在 Node-RED 论坛中发布的解决方法:

Workaround: Go to the http request node, select Use authentication, select basic authentication, leave username and password blank, select Done to save. On returning the 'use authentication' will be deselected. Authorization header should now be passed correctly when set in the incoming message.

图片来源:meesern