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

Change:

request.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');

to either Login or test depending on if in production or sandbox

request.setEndpoint('https://[login | test].salesforce.com/services/oauth2/token');

You also need to put the parameters in the body or as URL params and not header

request.setBody(
      'grant_type=password' + 
      '&client_id=xxxx' + 
      '&client_secret=xx' + 
      '&username=xx' + 
      '&password=xx'
);

I understood the concept now and thanks for sharing other links.

client_id, client_secret, username, password and grant_type should be sent in a HTTP POST body not in header.

HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');

String CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
String CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXX';
String USERNAME = 'XXXXXXXXXXXXXX';
String PASSWORD = 'XXXXXXXXXXXXXX';

req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
            '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);

Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('Body ' + response.getBody());
System.debug('Status ' + response.getStatus());
System.debug('Status code ' + response.getStatusCode());

Here is the output: enter image description here


Had the same error (SalesForce 2020). Under Manage Connected Apps, I had to make sure my selected OAuth Scopes were as follows

  • Access and manage your data (api)
  • Provide access to your data via the Web (web)
  • Allow access to your unique identifier (openid)

For the server, I had to make sure I was using my own domain which you can follow under

Setup / My Domain / Your Domain Name

NodeJS

const jsforce = require('jsforce');
const conn = new jsforce.Connection({
    loginUrl: 'https://......salesforce.com',
});

const username = '.....';
const password = 'user_password' + 'user_security_token';
conn.login(username, password, function (err, userInfo) {
    if (err) { return console.error(err); } else {
        console.log('connected');
    }
});

Note that password is a concatenation of the user's password and the user's Security Token.