【问题标题】:How do I make an Oauth2 REST request using Node and node-rest-client?如何使用 Node 和 node-rest-client 发出 Oauth2 REST 请求?
【发布时间】:2019-10-15 08:59:30
【问题描述】:

我正在尝试从文档中复制示例 POST 请求(https://www.npmjs.com/package/node-rest-clienthttps://www.npmjs.com/package/node-rest-client#basic-http-auth)。如何在 Node 中编码这个curl 请求?

$ 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 的价值所在。

【问题讨论】:

    标签: javascript node.js oauth node-rest-client


    【解决方案1】:

    我不得不更改标题值:

        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'                                       
    }                                                      
    

    【讨论】:

      猜你喜欢
      • 2017-10-01
      • 1970-01-01
      • 2015-07-18
      • 2021-05-08
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 2020-01-23
      相关资源
      最近更新 更多