头图

This article introduces some experience accumulated by the author in the process of using Restful API SDK in SAP Marketing Cloud work projects.

After successfully logging in to the SAP Marketing Cloud system, you can find the Marketing Cloud contact management application in the menu "Quick Start" -> "Manage Contacts". Click:

Here you can see a list of all contacts in the system.
The 1218377 on the left is the total number of system contacts, and the Create button just below it is the new button, which can be used to open the contact creation page. The search bar on the right is a Google-style fuzzy search entry.

If you use this interface for the first time, you need to pay attention to some small skills.

The four controls highlighted in the above figure are actually four filters. For example, there is no contact with the status of For Review in the current system, and the number is 0, so after clicking this filter:

The table will display 0 data. This is expected behavior by users, so don't be surprised if you see the form empty.

When the hyperlink of a certain contact data is clicked,

It will jump to the contact details page. The highlighted guid in the url below is the primary key value of this contact in the SAP database.

Use nodejs to modify the contact master data of Marketing Cloud

Suppose there is such a contact master data in Marketing Cloud:

Now the requirement is to modify the highlighted property of this contact instance using a programming language such as nodejs.
code show as below:

 var config = require("./mcConfig");
var request = require('request');

var url = config.tokenURL;

console.log("user: " + config.user + " password: " + config.password); 
var getTokenOptions = {
        url: url,
        method: "GET",
        json:true,     
        headers: {
            'Authorization': 'Basic ' + new Buffer(config.user + ":" + config.password).toString('base64'),
            "content-type": "application/json",
            "x-csrf-token" :"fetch"
        }
};

function getToken() {
  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: get csrf token via url: " + url );

      requestC(getTokenOptions,function(error,response,body){
       var csrfToken = response.headers['x-csrf-token'];
       if(!csrfToken){
          reject({message:"token fetch error: " + error});
          return;
       }
       console.log("Step1: csrf token got: " + csrfToken);
       resolve(csrfToken);
      }); 
     });
}

function updateContact(token){
    return new Promise(function(resolve, reject){
        var sPostData = "--batch_1f7d-bd35-caed" + "\n" + 
  "Content-Type: multipart/mixed; boundary=changeset_8f9e-9a44-9f9e" + "\n" + 
  "\n" + 
  "--changeset_8f9e-9a44-9f9e" + "\n" + 
  "Content-Type: application/http" + "\n" + 
  "Content-Transfer-Encoding: binary" + "\n" + 
  "\n" + 
  "MERGE Consumers('02000A21209F1EE99CDF1A1FC9AA8065')?sap-client=100 HTTP/1.1" + "\n" + 
  "Cache-Control: max-age=360" + "\n" + 
  "sap-contextid-accept: header" + "\n" + 
  "Accept: application/json" + "\n" + 
  "Accept-Language: en" + "\n" + 
  "DataServiceVersion: 2.0" + "\n" + 
  "MaxDataServiceVersion: 2.0" + "\n" + 
  "x-csrf-token: fQ2Pwfmf0K_LVYoKV9QYUw==" + "\n" + 
  "Content-Type: application/json" + "\n" + 
  //"Content-Length: 215" + "\n" + 
  "\n" + 
  "{\"YY1_CustomerType_ENH\":\"Jerry测试1\"}" + "\n" + 
  "--changeset_8f9e-9a44-9f9e--" + "\n" + 
  "\n" + 
  "--batch_1f7d-bd35-caed--";

        var requestC = request.defaults({jar: true});
    var createOptions = {
              url: config.updateContactURL,
              method: "POST",
              json:false,
              headers: {
                  "content-type": "multipart/mixed;boundary=batch_1f7d-bd35-caed",
                  'x-csrf-token': token
              },
              body:sPostData
        };
        requestC(createOptions,function(error,response,data){
            if(error){
                reject(error.message);
            }else {
               debugger;
               console.log("Contact updated successfully");
               resolve(data);
            }
        });
    });
}

getToken().then(updateContact).catch((error) =>{
  console.log("error: " + error.message);
});

I assign the field value that needs to be changed as "Jerry test 1" in the nodejs code:
This property is successfully updated after execution:

Modify SAP Marketing Cloud contact master data using postman

In the contact master data in Marketing Cloud, not all fields can be modified after successful creation. Fields in the Personal data area can be modified.

For example, I maintain some values in the "customer attribute" field:

Then click save:

The second batch operation is to read the data of multiple sub-nodes under the contact model through a roundtrip, which is not related to our modified scene.
Use postman to modify:

The body field maintains the following:

 --batch_1f7d-bd35-caed
Content-Type: multipart/mixed; boundary=changeset_8f9e-9a44-9f9e
--changeset_8f9e-9a44-9f9e
Content-Type: application/http
Content-Transfer-Encoding: binary
MERGE Consumers('02000A21209F1EE99CDF1A1FC9AA8065')?sap-client=100 HTTP/1.1
Cache-Control: max-age=360
sap-contextid-accept: header
Accept: application/json
Accept-Language: en
DataServiceVersion: 2.0
MaxDataServiceVersion: 2.0
x-csrf-token: fQ2Pwfmf0K_LVYoKV9QYUw==
Content-Type: application/json
Content-Length: 215
{"__metadata":{"uri":"https://jerry.hybris.com/sap/opu/odata/sap/CUAN_CONTACT_SRV/Consumers('02000A21209F1EE99CDF1A1FC9AA8065')","type":"CUAN_CONTACT_SRV.Consumer"},"YY1_CustomerType_ENH":"Jerry测试2"}
--changeset_8f9e-9a44-9f9e--
--batch_1f7d-bd35-caed--

The new value of the field I want to modify is: Jerry test 2

After executing postman, it is found that the value has been updated and the modification is successful

Create Marketing Cloud's contact data using nodejs

The source code is as follows:

 var config = require("./mcConfig");
var request = require('request');

var url = config.tokenURL;

console.log("user: " + config.user + " password: " + config.password); 
var getTokenOptions = {
        url: url,
        method: "GET",
        json:true,     
        headers: {
            'Authorization': 'Basic ' + new Buffer(config.user + ":" + config.password).toString('base64'),
            "content-type": "application/json",
            "x-csrf-token" :"fetch"
        }
};

function getToken() {
  return new Promise(function(resolve,reject){
      var requestC = request.defaults({jar: true});
      console.log("Step1: get csrf token via url: " + url );

      requestC(getTokenOptions,function(error,response,body){
       var csrfToken = response.headers['x-csrf-token'];
       if(!csrfToken){
          reject({message:"token fetch error: " + error});
          return;
       }
       console.log("Step1: csrf token got: " + csrfToken);
       resolve(csrfToken);
      }); 
     });
}

function createContact(token){
    return new Promise(function(resolve, reject){
        var oPostData = {"CountryCode":"CN",
                    "City":"Chengdu",
                    "FirstName":"Jerry4",
                    "LastName":"Wang2",
                    "PostalCode":"610093",
                    "RegionCode":"",
                    "Street":"天府软件园",
                    "HouseNumber":"天府软件园",
                    "DateofBirth":null,
                    "ContactPersonFacets":[
                      {"Id":"jerry1@sap.com",
                       "IdOrigin":"EMAIL",
                       "Obsolete":false,
                       "Invalid":false},
                       {"Id":"",
                       "IdOrigin":"PHONE",
                       "Obsolete":false,
                       "Invalid":false},
                       {"Id":"",
                       "IdOrigin":"MOBILE",
                       "Obsolete":false,
                       "Invalid":false},
                       {"Id":"",
                       "IdOrigin":"FAX",
                       "Obsolete":false,
                       "Invalid":false}
                       ],
                       "IsConsumer":true,
                       "Filter":{
                        "MarketingAreaId":"CXXGLOBAL"
                      }
                    };
        var requestC = request.defaults({jar: true});
        var createOptions = {
              url: config.createContactURL,
              method: "POST",
              json:true,
              headers: {
                  "content-type": "application/json",
                  'x-csrf-token': token
              },
              body:oPostData
        };
        requestC(createOptions,function(error,response,data){
            if(error){
                reject(error.message);
            }else {
               var oCreatedContact = data;
               console.log("created contact ID: " + oCreatedContact.d.ContactPersonId);
               resolve(data);
            }
        });
    });
}

getToken().then(createContact).catch((error) =>{
  console.log("error: " + error.message);
});

Here I hardcode the name field of the created contact to Jerry4:

Execute this js file with nodejs and output the successfully created contact guid:

See this successfully created contact on the Marketing Cloud UI:

Summarize

This article introduces the initialization method of SAP Marketing Cloud after logging in to the system for the first time, and a specific example of consuming the Marketing Cloud Restful API using common tools such as Node.js and Postman.


注销
1k 声望1.6k 粉丝

invalid