【问题标题】:curl succeeds with oath2 but fails with node.js requestcurl 使用 oauth2 成功,但使用 node.js 请求失败
【发布时间】: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

【问题讨论】:

  • 可能是因为您缺少标题吗?

标签: node.js curl


【解决方案1】:

我的第一个假设是您缺少请求所需的标头

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,
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    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);

如果这不正确,请告诉我,我会解决答案

【讨论】:

    猜你喜欢
    • 2016-09-19
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2022-01-24
    • 2014-10-11
    • 2021-03-24
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多