I have been trying to use native Node.JS code in Node 8 to get an access token from WSO2 with my client ID and client secret. I receive the following error: Unsupported Client Authentication Method!
Here is my code:
const querystring = require('querystring');
const https = require('https');
var postData = querystring.stringify({
'grant_type' : 'client_credentials'
});
var options = {
hostname: 'api.somedomain.com',
port: 443,
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (data) => {
process.stdout.write(data);
});
});
req.on('error', (err) => {
console.error(err);
});
req.write(postData);
req.end();
When I attempt to include another option parameter of 'auth' for the client ID and client secret then it tells me "TypeError: First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."
Any help on how to make this work is greatly appreciated.
Looks like you are missing the Authorization
header with the request. I'm no expert on javascript/node but the token generation works after adding the Authorization
header in the headers
section as below. I have used localhost for testing purpose.
var auth = 'Basic ' + Buffer.from("nM_ftrK2pjoBW4JofE21xI1cP0Ya" + ':' + "jmFJIgC5QMDkU_HxQKiDUbp5UAca").toString('base64');
var options = {
hostname: 'localhost',
port: 8243,
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
'Authorization': auth
}
};
The correct value (Authorization : Basic Base64(consumer-key:consumer-secret)
) should be passed in with the token request when invoking the token endpoint to get the access_token.