Lucas Gasenzer
Lucas Gasenzer

Reputation: 25

Turn my curl POST into NodeJS OAuth2 Token reques

I have two curl-strings that first do a OAuth2-Token-Request and then load data from the API. Now I want to include this into a node.js plugin so I need to do this from within the plugin.

CURL:

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)
});

The problem is that the only thing I get back is: { code: 405, message: 'Method not allowed' } whereas the CURL gives me the the right access_token.

Can anyone help? Thanks!!

Upvotes: 2

Views: 796

Answers (1)

rsp
rsp

Reputation: 111376

Maybe try:

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)
});

Upvotes: 2

Related Questions