I am trying to create an automation activity (SSJS) that would opt out or opt in a subscriber at a keyword level. I am using the code below and was able to run it in Automation Studio without errors. However, the Opt In Status does not seem to update. Any suggestions?
<script runat="server">
Platform.Load("Core", "1.1.1");
try {
/* get OAuth 2.0 access token */
var payload = '{"grant_type":"client_credentials",';
payload += '"client_id":"xxxxxx",';
payload += '"client_secret":"xxxxxx",';
payload += '"scope":"list_and_subscribers_read list_and_subscribers_write",';
payload += '"account_id":"xxxxxx"}';
var OAuth2URL = "https://xxxxxx.auth.marketingcloudapis.com/v2/token";
var contentType = 'application/json';
var accessTokenResult = HTTP.Post(OAuth2URL, contentType, payload);
var tokenObj = Platform.Function.ParseJSON(accessTokenResult["Response"][0]);
var accessToken = tokenObj.access_token;
var auth = ["Bearer " + accessToken];
/* create the payload */
var payload = {
"contactKey":"test1",
"attributeSets": [{
"name":"MobileConnect Subscriptions",
"items": [{
"values": [
{ "name":"Keyword", "value":"AB173FC3-3326-4ABB-A8B9-995D22E469C6" },
{ "name":"Mobile Number", "value":"61400000000" },
{ "name":"Opt In Status", "value":0 },
{ "name":"Opt Out Status", "value":1 }
]
}]
}]
};
/* make the API call and get the response */
var contactsUrl = "https://mcnm75lqhcrhj-ky-7jf9h275334.rest.marketingcloudapis.com/contacts/v1/contacts";
var req = new Script.Util.HttpRequest(contactsUrl);
req.emptyContentHandling = 0;
req.retries = 2;
req.continueOnError = true;
req.contentType = "application/json";
req.setHeader("Authorization", auth);
req.method = "patch";
req.postData = Stringify(payload);
var response = req.send();
Write(Stringify(response));
} catch(e) { Write(Stringify(e)); }
</script>
Also, I tried running the same code via cloudpages but I am getting this error: {"message":"Unable to retrieve security descriptor for this frame.","description":"System.InvalidOperationException: Unable to retrieve security descriptor for this frame. - from mscorlib\r\n\r\n"}
Thanks in advance