0

I can make a call to a sandbox Sales Cloud environment with Postman and retrieve a token.

However, when doing the same from a Cloud Page with SSJS it fails with

{"message":"An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.","description":"ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call. See inner exception for details.\r\n Error Code: OMM_FUNC_EXEC_ERROR\r\n - from Jint --> \r\n\r\n --- inner exception 1---\r\n\r\nSystem.Net.WebException: The remote server returned an error: (400) Bad Request. - from System\r\n\r\n\r\n\r\n"}

I've never had problems with this sort of code when calling Marketing Cloud API's, so I don't know what's the problem now with Sales Cloud:

   <script runat = "server" language = "JavaScript">        
    Platform.Load("Core", "1");

    var clientId = "MYDATA";
    var clientSecret = "MYDATA";

    var endpoint = "https://XXX-test.XXX/as/token.oauth2";
                    var payload = {
                      "grant_type": "client_credentials",
                      "client_id": clientId,
                      "client_secret": clientSecret
                    };
                    payload = Stringify(payload);

 try {
                      // Request a new token
                      var result = HTTP.Post(endpoint, "application/json", payload, ["Cache-Control"], ["no-cache"]);
    
                      if (result.StatusCode == 200) {
                        var response = Platform.Function.ParseJSON(result.Response[0]);
                        accessToken = response.access_token;
                        var expiresIn = response.expires_in;

                    }
    }
 else {
                 //LOG TO ERROR DE
                
        }
 catch (e) {
                    //LOG TO ERROR DE
                }
              

 </script>
3
  • Needs to be form-data content type Commented Jul 30, 2021 at 18:13
  • Thanks, actually the SSJS example worked for me salesforce.stackexchange.com/questions/225892/… var payload = 'grant_type=client_credentials&client_id='; payload +=clientId; payload +='&client_secret='; payload +=clientSecret; and using content application/x-www-form-urlencoded
    – Johhnytska
    Commented Jul 30, 2021 at 18:48
  • yes either form type works, but has to be sent over as form data Commented Jul 30, 2021 at 21:21

0

Browse other questions tagged .