javascript - 如何使用 Node 和 node-rest-client 发出 Oauth2 REST 请求?
问题描述
我正在尝试从文档中复制示例 POST 请求(https://www.npmjs.com/package/node-rest-client,https://www.npmjs.com/package/node-rest-client#基本-http-auth)。如何curl
在 Node 中编码这个请求?
$ curl -u user:password localhost:8080/oauth/token -d grant_type=client_credentials
{"access_token":"c6b53866-b38a-41f9-878c-87bb280ca9d8","token_type":"bearer","expires_in":43199,"scope":"write"}
我有这个
var oauth = {
data: { grant_type: "client_credentials" },
headers: { "Content-Type": "application/json" }
};
var options_auth = { user: "user", password: "password" };
var client = new Client(options_auth);
client.post("http://127.0.0.1:8080/oauth/token", oauth, function (data, response) {
// parsed response body as js object
console.log(data);
但这给了我一个错误
[2019-10-15T00:40:23.780Z] { error: 'invalid_request', error_description: 'Missing grant type' }
OAuth 服务器是 Spring Boot 的价值所在。
解决方案
我不得不更改标题值:
headers: { "Content-Type": "application/x-www-form-urlencoded" }
现在它给了我一个访问令牌:
[2019-10-15T00:58:15.354Z] {
access_token: 'c6b53866-b38a-41f9-878c-87bb280ca9d8',
token_type: 'bearer',
expires_in: 41873,
scope: 'write'
}
推荐阅读
- javascript - 如何在表格中定位此脚本?
- asp.net-core - 如何将 IViewLocationExtender 与 Razor 页面一起使用来呈现设备特定页面
- exception - 为什么在 corutineAScope 块中有 try/catch 仍然会因捕获的异常而崩溃
- slack - 如何一次从多个交互块中获取数据?
- android - Android Jsoup 查找特定元素
- ios - MapBox 在 CGRect 中获取 visibleCoordinateBounds
- angular - 如何从 Angular 中的另一个组件设置 FormGroup 值
- java - 不需要的 mockito
- c# - 如何使用 xamarin 表单中的集合视图添加 2 行不同的图像
- python - 如何在“th”语句中按关键字刮表