使用nodejs建立Marketing Cloud的contact資料

i042416發表於2019-05-25

原始碼如下:

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);
});

這裡我把建立的contact的名稱欄位硬編碼成Jerry4:

使用nodejs建立Marketing Cloud的contact資料

使用nodejs執行這個js檔案,輸出成功建立的contact guid:

使用nodejs建立Marketing Cloud的contact資料

在Marketing Cloud UI上看到這個建立成功的contact:

使用nodejs建立Marketing Cloud的contact資料

要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":


使用nodejs建立Marketing Cloud的contact資料


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2645492/,如需轉載,請註明出處,否則將追究法律責任。

相關文章