【发布时间】:2020-10-22 21:50:36
【问题描述】:
问题是我正在尝试检索 OAuth2 令牌。由于request 已被弃用,我为此使用node-fetch。虽然我可以让它与request 一起工作,但我不能与node-fetch 一起工作。
我已经阅读了很多关于这里的帖子,但似乎没有一个真正给出真正有效的一致答案。我承认我看起来还不够好。我将其包装在测试中可能也很复杂,但由于我收到的错误消息,感觉并非如此。
这是有效的代码(请注意,我必须更改详细信息以保护内部 URL):
var request = require("request");
var options = {
method: "POST",
url: "https://some-url/realms/my-realm/protocol/openid-connect/token",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
form: {
username: "JeffUser",
password: "jeff-password",
grant_type: "password",
client_id: "jeff-client",
client_secret: "jeff-client"
}
};
request(options, function(error, response, body) {
if (error) throw new Error(error);
console.log(body);
})
这行得通,我得到了令牌。这是我在node-fetch(包含在测试中)中尝试的失败:
const assert = require("chai").assert;
const fetch = require("node-fetch")
describe("Test API", function() {
let api_token = "";
it("gets a token", async function() {
api_token = await fetch("https://some-url/realms/my-realm/protocol/openid-connect/token", {
method: "post",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
form: {
username: "JeffUser",
password: "jeff-password",
grant_type: "password",
client_id: "jeff-client",
client_secret: "jeff-client"
}
}).then (response => {
return response.json();
});
console.log(token);
});
});
这里发生的是我从该测试中得到以下输出:
{
error: 'invalid_request',
error_description: 'Missing form parameter: grant_type'
}
我已尝试按照某些搜索的建议将 form 更改为 body,但这似乎没有任何效果。我尝试用 JSON.stringify() 包装 form: { } 数据,正如其他一些搜索所建议的那样,但这也导致了同样的错误被报告。
【问题讨论】:
-
它需要是
body而不是form。而且您可能需要自己处理正确格式的数据编码,不确定是否有任何自动为您做这件事。 -
github.com/node-fetch/node-fetch#post-with-form-parameters - 使用
URLSearchParams之类的东西以application/x-www-form-urlencoded要求的正确格式提供您的数据。 -
@CBroe:谢谢。但是不清楚为什么
request代码有效,我不使用“body”。此外,没有其他逻辑(我知道)专门针对编码做任何事情。基本上我正在尝试将request中的工作内容移植到node-fetch。我会试试URLSearchParams的事情。 -
就是这样!
URLSearchParams是我需要的。谢谢!
标签: javascript oauth-2.0 node-fetch