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>