【发布时间】:2017-08-18 06:35:39
【问题描述】:
以下 curl 请求正常工作,返回应有的身份验证令牌
curl POST -s -d "grant_type=password&username=someusername&password=somepassword&client_id=someid&client_secret=somesecret&response_type=token" "someurl"
当 node.js 中的等效请求失败时
let url = someurl
var dataString = 'grant_type=password&username=somename&password=somepassword&client_id=someid&client_secret=somesecret&response_type=token';
var options = {
url: url,
method: 'POST',
body: dataString,
allow_redirects : true
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
console.log(error)
console.log(body)
}
request(options, callback);
错误是
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}
这可能只特定于此代码。无论如何,什么差异(可能是默认参数或配置)可以解释这种差异。请注意,无论是否使用 allow_redirects 选项,程序都会失败,但应该允许重定向。
这是在 macosx 上运行的节点 v7.10.0 并请求 2.81.0
【问题讨论】:
-
可能是因为您缺少标题吗?