使用nodejs创建Marketing Cloud的contact数据

in 编程
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

源代码如下:

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执行这个js文件,输出成功创建的contact guid:

在Marketing Cloud UI上看到这个创建成功的contact:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看