目前,我正在为RocketChat开发一个用nodejs编写的简单服务。但是模块“请求”不会发送我的标头参数进行身份验证。详细信息如下:
//Login
request({
url: rocketChatURL + '/api/v1/login',
method: 'POST',
body: JSON.stringify({
'user': rocketChatUser,
'password': rocketChatPass
})
}, (err, res, body) => {
if (err) { return console.log(err); }
console.log(body);
obj = JSON.parse(body);
console.log(obj);
rocketChatAuthToken = JSON.stringify(obj.data.authToken);
});
//Channel Infos
var options = {
url: rocketChatURL + '/api/v1/channels.info?roomName=' + rocketChatRoom,
method: 'GET',
//json: true,
headers: {
"X-User-Id": rocketChatUserID,
"X-Auth-Token": rocketChatAuthToken,
}
};
登录返回以下对象:
{ status: 'success',
data:
{ userId: 'XYZ',
authToken: 'JS_VNjOnFpicTIdhD4n1WOdvl950wOEa-LDDACqg_yN',
me:
{ _id: 'XYZ',
name: 'myTechuser',
emails: [Array],
status: 'online',
statusConnection: 'online',
username: 'myTechuser',
utcOffset: 2,
active: true,
roles: [Array],
settings: [Object],
email: 'xxx@yyy.com'
}
}
}
首先登录。之后,我想获取一些频道信息。因此,必须通过标头提供UserID和AuthToken(请参见https://rocket.chat/docs/developer-guides/rest-api/channels/info/)。响应始终是带有错误消息“您必须登录才能执行此操作”的HTTP 401。 经过对“ request-debug”的研究,我发现请求模块仅发送“ X-User-Id”标头,而不发送“ X-Auth-Token”(请参见下面的代码段)。 这就是身份验证失败的原因。因为如果我通过CURL(当然还有标头参数)手动发送请求,那么身份验证就可以正常工作。
您能向我解释为什么请求模块不随请求一起发送第二个标头选项吗?
{ request:
{ debugId: 3,
uri:
'https://chat.xxxxx.com/api/v1/channels.info?roomName=myRoom',
method: 'GET',
headers:
{ 'X-User-Id': 'XYZ',
host: 'chat.xxxxx.com' } } }
{ response:
{ debugId: 3,
headers:
{ date: 'Mon, 29 Apr 2019 19:02:03 GMT',
'content-type': 'application/json',
'transfer-encoding': 'chunked',
connection: 'close',
server: 'nginx/1.13.12',
'x-instance-id': 'g47SaWLkqdM2ZrhQ8',
'access-control-allow-origin': '*',
'cache-control': 'no-store',
pragma: 'no-cache',
vary: 'Accept-Encoding',
'set-cookie': [Array],
'strict-transport-security': 'max-age=31536000; includeSubDomains' },
statusCode: 401,
body:
'{"status":"error","message":"You must be logged in to do this."}' } }