I am trying to retrieve a template-based email asset using the Content Builder API via a Cloud page. I am having trouble retrieving the template. I am not sure if it is an issue with the structure of the payload or a potential issue with the HTTP Get method. I am getting the following error message:
Unable to retrieve security descriptor for this frame.Failed to retrieve email template.
This is the code I have been using to make the request:
Platform.Load("core", "1");
var clientId = "{{REMOVED}}";
var clientSecret = "{{REMOVED}}";
var grantType = "{{REMOVED}}";
var accountId = "{{REMOVED}}";
try
{
var success = false;
var accessToken = GetAccessTokenForAPI(clientId, clientSecret, grantType, accountId);
if (accessToken != null)
{
success = retrieveEmailTemplate(accessToken);
}
if (success) {
Write("Retrieved email template.");
} else {
Write("Failed to retrieve email template.");
}
}
catch (exc)
{
Write("Exception Error: " + Stringify(exc));
}
function GetAccessTokenForAPI(clientId, clientSecret, grantType, accountId)
{
var url = 'https://{{REMOVED}}.auth.marketingcloudapis.com/v2/token';
var contentType = 'application/json';
var payload =
{
client_id: clientId,
client_secret: clientSecret,
grant_type: grantType,
account_id: accountId
};
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if (accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
return tokenResponse.access_token;
}
else
{
return null;
Write("Error");
}
}
function retrieveEmailTemplate(accessToken)
{
var url = 'https://{{REMOVED}}.rest.marketingcloudapis.com/asset/v1/content/assets/query';
var contentType = 'application/json';
var headername = ["Authorization"];
var headervalue = ["Bearer " + accessToken];
var payload ='{"query":{"leftOperand":{"property":"name","simpleOperator":"equal","value":"{{REMOVED}}"},"logicalOperator":"AND","rightOperand":{"property":"assetType.name","simpleOperator":"equal","value":"templatebasedemail"}},"fields":["name","views","thumbnail","category","content","data"]}';
try {
var emailRequest = HTTP.Get(url, contentType, payload, headername, headervalue);
var emailResult = Platform.Response.Write("Email request: " + emailRequest);
return emailResult;
}
catch(err) {
Platform.Response.Write("This is an error: " + err);
}
}
Best Answer
The simple query version is a GET, but you need to use POST for the Advanced Query version.
Using HTTP.POST instead will likely get you the expected results.