0

I'm have to call an API that use OAuth2 with Client Credentials.

I'm having some trouble to do it...

This is the code I produce (using request package) :

const credentials = {
    client: {
        id: 'MY_ID',
        secret: 'My_PASSWORD'
    },
    auth: {
        tokenHost: 'DOMAIN',
        tokenPath: 'PATH',
        scope: '',
        grantType: "client_credentials"
    }
};

var options = { 
    method: 'POST',
    url: credentials.auth.tokenHost + credentials.auth.tokenPath,
    headers: { 'content-type': 'application/json' },
    body: { 
        grant_type: credentials.auth.grantType,
        client_id: credentials.client.id,
        client_secret: credentials.client.secret
    },
    json: true
};

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

I have this error :

{ error: 'invalid_request',
  error_description: 'Missing form parameter: grant_type' }

Maybe I'm missing something.

It would be very nice of you if you can help me to figure it out :)

PS : It works on Postman so my values are correct.

elition
  • 41
  • 1
  • 7
  • Body should be string.You should use this syntax https://stackoverflow.com/a/38989671/1194525 – bato3 Feb 11 '19 at 14:46
  • Possible duplicate of [POST data with request module on Node.JS](https://stackoverflow.com/questions/6432693/post-data-with-request-module-on-node-js) – bato3 Feb 11 '19 at 14:47
  • It was in String format. I finaly decide to use Axios instead. It solved my problem. – elition Feb 12 '19 at 09:29

0 Answers0