1

I am having some issues with my node.js app that I have been testing. This app is supposed to authenticate with my Salesforce Sandbox using a JWT in order to get a new access token. The token is working, as I am able to take the token from my node.js app and use it in a Postman call.

However, whenever I try to use my node.js app to perform the call to salesforce, I keep getting the following back:

{"error":"unsupported_grant_type","error_description":"grant type not supported"}

I have gone over my code several times and I don't see why it would be causing this error. Everything online seems to suggest that I am not passing in the correct Content-Type Header but I am definitely doing that.

    var https = require('https');
    var jwt = require('jsonwebtoken');

    var data = JSON.stringify({
        'grant_type' : 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion' : token
    });

    var options = {
        host: 'test.salesforce.com',
        path: '/services/oauth2/token',
        method: 'POST',
        headers: {
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Content-Length' : data.length
        }
    };

    console.log(options);

    var req = https.request(options, (res) => {
        console.log('statusCode: ' + res.statusCode);

        res.on('data',(d) => {
            console.log(d.toString());
        });
    });

    req.write(data);
    req.end();

Any help is greatly appreciated, thanks!

1 Answer 1

0

Oh my I must have had blinders on.

Figured out the problem! Even though I was passing in my Content-Type of 'application/x-www-url-encoded', I wasn't actually passing in a valid body. I was able to resolve my issue with the following additions:

At the top of my node.js app:

var querystring = require('querystring');

Updated the var data assignment:

var data = querystring.stringify({
    grant_type : 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    assertion : token
});

Everything is now working.

2
  • 1
    Right. You should also use request as your http client, it might have prevented you from making this mistake - request.post('https://test.salesforce.com/services/oauth2/token', {form:{grant_type:'urn:ietf:params:oauth:grant-type:jwt-bearer',...})
    – identigral
    Aug 26, 2019 at 15:58
  • It seems that any number of issues can cause the unsupported_grant_type - there is another SO question that points to headers being the issue. I have not found my issue yet, but this is helpful.
    – digidigo
    Mar 4, 2023 at 0:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .