I am trying to get access token using SSJS, but to retrieve client id and client secret I want to use an ampscript function - DecryptSymmetric.
When I use this function I get '500 - Internal server error'.
<script runat="server">
var fullToken = getToken()
function ampScript(code) {
var ampBlock = '\%\%[' + code + ']\%\%';
Platform.Function.TreatAsContent(ampBlock);
return Variable.GetValue('@response');
};
function getToken(){
var clientId = ampScript("SET @response = DecryptSymmetric('enctryptedClientId', 'AES','symmetricKeyValue', @null,'saltKeyValue', @null, 'InitializationVectorKeyValue', @null");
var clientSecret = ampScript("SET @response = DecryptSymmetric('enctryptedClientSecret', 'AES','symmetricKeyValue', @null,'saltKeyValue', @null, 'InitializationVectorKeyValue', @null");
var authEndpoint = 'xyz.auth.marketingcloudapis.com';
var payload = {
client_id: clientId,
client_secret: clientSecret,
grant_type: "client_credentials"
};
var url = authEndpoint + '/v2/token';
var contentType = 'application/json';
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if(accessTokenRequest.StatusCode == 200) {
var accessToken = Platform.Function.ParseJSON(accessTokenRequest["Response"][0]).access_token;
var token = 'Bearer ' + accessToken;
}
return token;
}
</script>
var ampBlock = '%'+'%[' + code + ']%' + '%';
instead. I have found those cause me more headaches then anything in SSJS. Also wrap it in a try/catch to see if you can get more details on the error.