I'm trying to obtain an OAuth 2 token through SSJS and getting a 400 error.
Here's the code I'm using:
<script type="javascript" runat="server">
Platform.Load("Core", "1.1.5");
var logDE = "{DE_ExtKey}";
var log = DataExtension.Init(logDE);
//get authorization token
var url = "https://{TenantSpecificEndpoint}.auth.marketingcloudapis.com/V2/Token";
var contentType = "application/json";
var payload = '{"grant_type":"client_credentials","clientId":"{ClientIDValue}","clientSecret":"{ClienrSecretValue}"}';
try {
var accessTokenResult = HTTP.Post(url, contentType, payload);
var statusCode = accessTokenResult["StatusCode"];
var response = accessTokenResult["Response"][0];
var accessToken = platform.Function.ParseJSON(
accessTokenResult["Response"][0]
).access_token;
result = Stringify(accessToken).replace(/[\n\r]/g, "");
log.Rows.Add({ Message: "result: " + result });
} catch (e) {
e = Stringify(e).replace(/[\n\r]/g, "");
log.Rows.Add({ Message: "ErrorMsg: " + e });
}
</script>
I'm not sure if I'm missing something or if this just isn't supported (yet)? The error message description contains the following: "ExactTarget.OMM.FunctionExecutionException: An error occurred when attempting to evaluate a HTTPPost function call." If I swap out the credentials and endpoint for the "classic" OAuth token this works just fine but as the ability to create new credentials using the legacy functionality is going away in a few days it would be good to get this working with OAuth 2.