1

I am new to marketing cloud, can anyone tell me how to automate contactdeletion process using new OAuth 2.0. Using legacy the process works but not able to proceed with OAuth 2.0.

<script runat="server">
Platform.Load("Core","1.1.1");
var authEndpoint = 'https://mc6ktt9crwdpg55f3p-9j1- 
1kfyq.auth.marketingcloudapis.com';
var payload = {
    client_id: "Client ID",
    client_secret: "Client secret",
grant_type: "client_credentials"
};
var url = authEndpoint + '/v2/token';  
var contentType = 'application/json';


try {
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
if(accessTokenRequest.StatusCode == 200) {
    var tokenResponse = 
 Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
    var accessToken = tokenResponse.access_token;
   }

 } 
catch (error) {
Write(Stringify(error));
}


if(accessToken !='')
{

 var contentType1 = 'application/json';
 var restUrl = 'https://mc6ktt9crwdpg55f3p-9j1- 
 1kfyq.rest.marketingcloudapis.com/';
 var headerNames = ["Authorization"];
 var headerValues = ["Bearer " + accessToken];

 payload1 =  '{';
 payload1 += '   "deleteOperationType": "ContactAndAttributes",';
 payload1 += '   "targetList": { ';
 payload1 += '      "listType": { ';
 payload1 += '         "listTypeID":3';
 payload1 += '      },';
 payload1 += '   "listKey": "' + 'DE Customer Key' + '"';
 payload1 += '   },';
 payload1 += '   "deleteListWhenCompleted":false,';
 payload1 += '   "deleteListContentsWhenCompleted":true';
 payload1 += '}';

 var endpoint = restUrl + 'contacts/v1/contacts/actions/delete? 
 type=listReference';

  }
  try 
 {
  var result = HTTP.Post(endpoint, contentType1, payload1, headerNames, 
  headerValues)
  }
 catch (e) {

 Write(Stringify(e));

 }

 </script>

code results in 401 error

1 Answer 1

6

For v2 you need to use /v2/token and the payload is slightly different - you need to include grant_type:

{
    client_id: "insert Client Id",
    client_secret: "insert Client Secret",
    grant_type: "client_credentials",
    scope: "list_and_subscribers_write"
  }

Here's the whole thing:

Platform.Load("Core","1.1.1");

var authEndpoint = 'https://mcxxxxxxxxxxxxxxxxxxxxxxxxx.auth.marketingcloudapis.com';
var payload = {
        client_id: "insert Client Id",
        client_secret: "insert Client Secret",
        grant_type: "client_credentials",
        scope: "list_and_subscribers_write"
    };
var url = authEndpoint + '/v2/token';  
var contentType = 'application/json';

try {
   var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
   if(accessTokenRequest.StatusCode == 200) {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.access_token;
   }
} catch (error) {
    Write(Stringify(error));
}

Note that for v2, the access token has an underscore: access_token. The rest of your process should work fine.

Here's an article where you can read about v1 and v2 authorisation in MC using SSJS: Salesforce Marketing Cloud API Authentication using Server-Side JavaScript


Try this for the Contact Delete part:

var deKey = '99E99999-2E8B-4D2C-AF6C-F84DD64C04B0';
var contentType = 'application/json';
var restUrl = tokenResponse.rest_instance_url;
var headerNames = ["Authorization"];
var headerValues = ["Bearer " + accessToken];

payload =  '{';
payload += '   "deleteOperationType": "ContactAndAttributes",';
payload += '   "targetList": { ';
payload += '      "listType": { ';
payload += '         "listTypeID":3';
payload += '      },';
payload += '   "listKey": "' + deKey + '"';
payload += '   },';
payload += '   "deleteListWhenCompleted":false,';
payload += '   "deleteListContentsWhenCompleted":true';
payload += '}';

var endpoint = restUrl + 'contacts/v1/contacts/actions/delete?type=listReference';

var result = HTTP.Post(endpoint, contentType, payload, headerNames, headerValues)
1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Commented Nov 22, 2019 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .