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

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

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.
  • 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"} – user4567570 May 4 '17 at 15:03
  • @user That's kind of a completely different question. You've now got the callout part working. – Adrian Larson May 4 '17 at 15:06
  • That said, if you sift through salesforce.stackexchange.com/questions/23239/… you need to specify a callback_url header. – Adrian Larson May 4 '17 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 May 4 '17 at 15:09

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.