关于javascript:SAP-Marketing-Cloud-Restful-API-SDK-使用案例分享

4次阅读

共计 5689 个字符,预计需要花费 15 分钟才能阅读完成。

本文介绍笔者在 SAP Marketing Cloud 工作我的项目中应用 Restful API SDK 过程中积攒的一些应用教训。

胜利登录 SAP Marketing Cloud 零碎之后,能够在菜单 ” 疾速启动 ”->”Manage Contacts” 里找到 Marketing Cloud contact 治理利用。单击:

这里就能看到该零碎里所有的 contact 列表了。
右边的 1218377 是零碎 contact 总个数,正下方 Create 就是新建按钮,能够通过这个按钮关上 contact 创立页面。左边的 search bar 就是一个 Google 格调的含糊搜寻入口。

这个界面第一次应用的话须要留神一些小技巧。

上图高亮的四个控件实际上是四个过滤器,例如以后零碎里并不存在状态为 For Review 的 contact,数字为 0,因而单击这个过滤器后:

表格会显示 0 条数据。这是用户冀望的行为,因而大家如果看到表格是空的,不要感觉奇怪。

当单击某条 contact 数据的超链接后,

会跳转到 contact 明细页面. 下图 url 里高亮的 guid 就是这条 contact 在 SAP 数据库里的主键值。

应用 nodejs 对 Marketing Cloud 的 contact 主数据进行批改操作

假如在 Marketing Cloud 有这样一个 contact 主数据:

当初需要是应用编程语言比方 nodejs 批改这个 contact 实例的高亮属性。
代码如下:

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

我在 nodejs 代码里把须要更改的字段值赋为 ”Jerry 测试 1”:
执行之后这个属性被胜利更新了:

应用 postman 批改 SAP Marketing Cloud contact 主数据

Marketing Cloud 里的 contact 主数据,创立胜利后也不是所有字段都可能被批改。在 Personal data 区域的字段是能够被批改的。

比方我在“客户属性”字段里保护了一些值:

而后点保留:

其中第二个 batch 操作是通过一个 roundtrip 读取 contact 模型下多个子节点的数据,和咱们这个批改的场景没有关联。
应用 postman 进行批改:

body 字段保护以下内容:

--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--

我想批改的字段的新的值为:Jerry 测试 2

执行 postman 后,发现值曾经更新了,批改胜利

应用 nodejs 创立 Marketing Cloud 的 contact 数据

源代码如下:

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:

总结

本文介绍了 SAP Marketing Cloud 在第一次登陆零碎后的初始化形式,以及应用 Node.js 和 Postman 等常用工具,生产 Marketing Cloud Restful API 的具体例子。

正文完
 0