3

I am getting the below error when trying to call the HTTP POST using the Apex

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');
request.setMethod('POST');

request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
request.setHeader('client_id','3MVG9d8..z.hDcPJZPXXXXXXXXXXXXXXXXXXX');
request.setHeader('client_secret','XXXXXXXXXXXXXXXXXX');
request.setHeader('username','XXXXXXXXXXXXXXXXXX');
request.setHeader('password','XXXXXXXXXXXXXXXXXXXXXX');
request.setHeader('grant_type','password');
request.setHeader('callbackURL','https://ap5.salesforce.com/oauthcallback.html');

request.setBody('');

HttpResponse response = http.send(request);
System.debug('Response :: '+response.getBody());

And I get the error message.

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

I am able to call successfully using REST/POSTMAN client and getting the access_token.

  • Just a note, the [apexrest] tag is for when you are writing an ApexClass that will serve as a @RestResource, which does not appear to be the case here. – Adrian Larson May 4 '17 at 16:10
  • See answer here for troubleshooting: salesforce.stackexchange.com/questions/172800/… - Also you do not need to provide the callback URL for the UN PW flow – Eric May 4 '17 at 16:26
  • @user4567570 Since you seem to be new here, just wanted to point out that it is considered being a good citizen to accept an answer if it solve your issue. You are under no obligation to do so but it helps others find answer to similar questions and also shows your appreciation to to efforts towards the person who spent time providing you with the answer – Eric May 4 '17 at 18:20
7

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'
);
1

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

  • Not really considered being a good citizen to post your own answer and accept it after accepting another answer when it does not offer any additional information than existing answers...... – Eric May 9 '17 at 17:43
-1

Also I found this as the simplest way.

Map<String, String> request = new Map<String, String>();
request.put('client_id','XXXXXXXXXXXX');
request.put('client_secret','XX');
request.put('username','XXX');
request.put('password','VXXXX');
request.put('grant_type','password');

String mapString = String.valueOf(request).replace(',','&')
    .replace(' ','').replace('{','').replace('}','');
System.debug('Request String '+mapString);

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');
req.setBody(mapString);

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.