So first of all you need to perform login
action to retrieve fuel OAuth token.
Request format is following due to the documentation
POST https://auth.exacttargetapis.com/v1/requestToken
Content-Type: application/json
{
"clientId": "YOUR_CLIENT_ID",
"clientSecret": "YOUR_CLIENT_SECRET"
}
Response is following:
200 OK
{
"accessToken": "YOUR_ACCESS_TOKEN"
"expiresIn": 3600
}
To perform such callouts, you need some Apex class with the following method
private static String makeSimpleJSONPostCall(String endPoint, String soapBody){
Http h = new Http();
HttpRequest r = new HttpRequest();
r.setTimeout(60000);
r.setEndpoint(endPoint);
r.setMethod('POST');
r.setHeader('Content-type','application/json');
r.setBody(soapBody);
HttpResponse res = h.send(r);
return r.getBody();
}
Now you can perform get OAuth token request using the following form
private static String MARKETING_CLOUD_AUTH_URL = 'https://auth.exacttargetapis.com/v1/reaquestToken';
private static String ACCESS_TOKEN = 'accessToken';
private static String getMarketingCloudOAuthToken(String clientId, String clientSecret){
String responseBody = makeSimpleJSONPostCall(
MARKETING_CLOUD_AUTH_URL,
JSON.serialize( new Map<String, String>{
'clientId' => clientId,
'clientSecret' => clientSecret
} )
);
return ((Map<String, String>) JSON.deserialize(responseBody, Map<String, String>.class)).get( ACCESS_TOKEN );
}
The method above assumes successful authorization. You might consider case when res.getStatusCode() != 200
, in such case method getMarketingCloudOAuthToken
might throw an exception.
Next, you need to send SOAP Envelope example from the document you provided
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<Security xmlns="https://www.marketingcloud.com/">
<fueloauth>YOUR_ACCESS_TOKEN</fueloauth>
</Security>
</Header>
<Body>
<UpdateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
<Objects xsi:type="Subscriber">
<PartnerKey xsi:nil="true"/>
<ObjectID xsi:nil="true"/>
<EmailAddress>example@example.com</EmailAddress>
<Lists>
<PartnerKey xsi:nil="true"/>
<ObjectID xsi:nil="true"/>
<ID>123456</ID>
<Status>Active</Status>
<Action>create</Action>
</Lists>
</Objects>
</UpdateRequest>
</Body>
</Envelope>
You just need to perform HTTPCallout from Apex like this
private static String makeHTTPXMLPost(String endPoint, String soapBody){
Http h = new Http();
HttpRequest r = new HttpRequest();
r.setTimeout(60000);
r.setEndpoint(endPoint);
r.setMethod('POST');
r.setHeader('SOAPAction','DoesNotMatter');
r.setHeader('Accept','text/xml');
r.setHeader('Content-type','text/xml');
r.setHeader('charset','UTF-8');
reqLLogin.setBody(soapBody);
HttpResponse s = h.send(r);
return s.getBody();
}
You can use makeHTTPXMLPost
method in the following format
private static String performMCAction(String accessToken, String email, String listId, String status, String action){
String soapEnvelopeTemplate = ' <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
' <Header>'+
' <Security xmlns="https://www.marketingcloud.com/">'+
' <fueloauth>{0}</fueloauth>'+
' </Security>'+
' </Header>'+
' <Body>'+
' <UpdateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">'+
' <Objects xsi:type="Subscriber">'+
' <PartnerKey xsi:nil="true"/>'+
' <ObjectID xsi:nil="true"/>'+
' <EmailAddress>{1}</EmailAddress>'+
' <Lists>'+
' <PartnerKey xsi:nil="true"/>'+
' <ObjectID xsi:nil="true"/>'+
' <ID>{2}</ID>'+
' <Status>{3}</Status>'+
' <Action>{4}</Action>'+
' </Lists>'+
' </Objects>'+
' </UpdateRequest>'+
' </Body>'+
' </Envelope>';
String body = String.format(template, new String[]{accessToken, email, listId, status, action});
return makeHTTPXMLPost( 'https://auth.exacttargetapis.com/' , body );
}
I hope this would help.
Important point: to make Apex HTTP Callout work, you need to add marketing cloud URL to remote site settings. You can do this following this link:
https://login.salesforce.com/0rp/e?EndpointUrl=https://auth.exacttargetapis.com/&SiteName=MarketingCloud
for production and developer edition instances or this link
https://test.salesforce.com/0rp/e?EndpointUrl=https://auth.exacttargetapis.com/&SiteName=MarketingCloud
for sandboxes and after you follow this link click save to add marketing cloud URL to remote site settings.