【问题标题】:Turn my curl POST into NodeJS OAuth2 Token reques将我的 curl POST 转换为 Node JS OAuth2 Token 请求
【发布时间】:2017-04-05 09:30:13
【问题描述】:

我有两个 curl-strings,它们首先执行 OAuth2-Token-Request,然后从 API 加载数据。现在我想将它包含到 node.js 插件中,所以我需要在插件中执行此操作。

卷曲:

curl -X POST -d 'grant_type=password&client_id=8d3c1664-05ae-47e4-bcdb-477489590aa4&client_secret=4f771f6f-5c10-4104-bbc6-3333f5b11bf9&username=email&password=password' https://api.hello.is/v1/oauth2/token

Test.js:

var request = require('request');

request({
    url: 'https://api.hello.is/v1/oauth2/token',
    mehtod: "POST",
    auth: {
        username: 'email',
        password: 'password'
    },
    form: {
        'grant_type': 'password',
        'client_id': '8d3c1664-05ae-47e4-bcdb-477489590aa4',
        'client_secret': '4f771f6f-5c10-4104-bbc6-3333f5b11bf9'
    }
}, function(err, res) {
    var json = JSON.parse(res.body);
    console.log("Access Token:", json.access_token)
});

问题是我唯一返回的是:{ code: 405, message: 'Method not allowed' } 而 CURL 给了我正确的 access_token。

有人可以帮忙吗?谢谢!!

【问题讨论】:

  • 您的代码中似乎有错字:mehtod => method
  • 哈哈。这样的菜鸟错误......现在它说“{代码:401,消息:'未授权。' }"

标签: node.js curl oauth-2.0 access-token


【解决方案1】:

不妨试试:

var request = require('request');

request({
    url: 'https://api.hello.is/v1/oauth2/token',
    mehtod: "POST",
    form: {
        username: 'email',
        password: 'password',
        grant_type: 'password',
        client_id: '8d3c1664-05ae-47e4-bcdb-477489590aa4',
        client_secret: '4f771f6f-5c10-4104-bbc6-3333f5b11bf9'
    }
}, function(err, res) {
    var json = JSON.parse(res.body);
    console.log("Access Token:", json.access_token)
});

【讨论】:

  • 坦克!那行得通……从在线教程中复制有点快乐;)
猜你喜欢
  • 2021-07-20
  • 2021-02-14
  • 2023-03-20
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多