3

I am new to Salesforce but aware about the REST. I'm trying to make the HTTP POST call to get the access_token first to call the actual GET, PUT and POST service.

I have written the following code in developer console and same will try in test class.

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/json;charset=UTF-8');
request.setHeader('client_id','3MVG9d8..z.hDcPJZPIzGJ5UZDrL7bJthAQ_v0YCxzEeG1XXXXXXXXXXXXXXXXXXXXX');
request.setHeader('client_secret','XXXXXXXXXXX');
request.setHeader('username','XXXXXXXXXXXXX.com');
request.setHeader('password','XXXXXXXXXXXXX');
request.setHeader('grant_type','password');

request.setBody('');

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

I am not sure when if we need to send above data in header or body? But when I tried using header I get the following error. Please guide further.

Line: 15, Column: 1
System.CalloutException: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://ap5.salesforce.com/services/oauth2/token

When I tried in REST Client, it works fine.

enter image description here

1
  • It's because you're making the callout from Apex.
    – Adrian Larson
    Commented May 4, 2017 at 14:59

1 Answer 1

7

The error message tells you exactly what you need to do. Before making any Http callout, you must first authorize the endpoint by adding it to your Remote Site Settings. Navigate to Setup > Security > Remote Site Settings, then add https://ap5.salesforce.com:

New Remote Site

See also: Adding Remote Site Settings:

Before any Apex callout can call an external site, that site must be registered in the Remote Site Settings page, or the callout fails. Salesforce prevents calls to unauthorized network addresses.

Note
If the callout specifies a named credential as the endpoint, you don’t need to configure remote site settings. A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. To set up named credentials, see “Define a Named Credential” in the Salesforce Help.

To add a remote site setting:

  1. From Setup, enter Remote Site Settings in the Quick Find box, then select Remote Site Settings.
  2. Click New Remote Site.
  3. Enter a descriptive term for the Remote Site Name.
  4. Enter the URL for the remote site.
  5. Optionally, enter a description of the site.
  6. Click Save.
4
  • Thank you. But now see the error: 20:31:55:021 USER_DEBUG [16]|DEBUG|Response :: {"error":"unsupported_grant_type","error_description":"grant type not supported"} Commented May 4, 2017 at 15:03
  • @user That's kind of a completely different question. You've now got the callout part working.
    – Adrian Larson
    Commented May 4, 2017 at 15:06
  • That said, if you sift through salesforce.stackexchange.com/questions/23239/… you need to specify a callback_url header.
    – Adrian Larson
    Commented May 4, 2017 at 15:07
  • Also take a look at salesforce.stackexchange.com/questions/5925/…, which indicates perhaps you should set your content-type header to application/x-www-form-urlencoded. Regardless, unsupported_grant_type is a completely separate error, and the fact you are getting that far means you have solved your initial issue. Better SFSE etiquette would be to post a separate question for it. If you keep changing your question as you encounter a new error, it makes your questions hard to answer.
    – Adrian Larson
    Commented May 4, 2017 at 15:09

You must log in to answer this question.

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