如何用Java代码在SAP Marketing Cloud里创建contact数据
我们可以使用SAP Marketing Cloud提供的Contact create OData API在第三方应用里创建Contact主数据.
API地址:/sap/opu/odata/sap/CUAN_CONTACT_SRV/InteractionContacts
示例代码只有100多行:
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import sun.misc.BASE64Encoder;
public class SimpleContactCreator {
	private ConfigUtil mConfigUtil = new ConfigUtil();
	HttpClient m_httpClient;
	private String getBasicAuth(){
		final String text = mConfigUtil.getConfig("user") + ":" + mConfigUtil.getConfig("password");
		BASE64Encoder encoder = new BASE64Encoder();
		String credentials = "basic " + encoder.encode(text.getBytes());
		return credentials;
	}
	private HttpClient getHttpClient() {
		if (this.m_httpClient == null) {
			this.m_httpClient = HttpClientBuilder.create().build();
		}
		return this.m_httpClient;
	}
	private String getCSRFToken(){
		String url = mConfigUtil.getConfig("tokenurl");
		System.out.println("fetch CSRF token via url: " + url);
		final HttpGet get = new HttpGet(url);
		get.setHeader("Authorization", getBasicAuth());
		get.setHeader("Cache-Control", "no-cache");
		get.setHeader("content-type", "application/json");
		get.setHeader("Accept", "application/json");
		get.setHeader("x-csrf-token", "fetch");
		HttpResponse response;
		String token = null;
		try {
			response = getHttpClient().execute(get);
			StatusLine statusLine = response.getStatusLine();
			int code = statusLine.getStatusCode();
			System.out.println("Status code: " + code);
			System.out.println("reason: " + statusLine.getReasonPhrase());
			token = response.getFirstHeader("x-csrf-token").getValue();
			System.out.println("token: " + token);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException | UnsupportedOperationException e) {
			e.printStackTrace();
		}
		return token;
	}
	public void run(String body){
		String token = getCSRFToken();
		createContact(token, body);
	}
	private void createContact(String token, String body){
		final HttpPost post = new HttpPost(
			URI.create(mConfigUtil.getConfig("contactcreateurl")));
		post.setHeader("Authorization", getBasicAuth());
		post.setHeader("Content-Type", "application/json");
		post.setHeader("X-CSRF-Token", token);
		HttpEntity entity = null;
		try {
			entity = new StringEntity(body);
		} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
		}
		post.setEntity(entity);
		HttpResponse response = null;
		try {
			response = getHttpClient().execute(post);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Response statusCode for Batch => "
			+ response.getStatusLine().getStatusCode());
	}
	public static void main(String[] args) {
		SimpleContactCreator tool = new SimpleContactCreator();
		String body = "{\"IsConsumer\":true," +
                      "\"Filter\":{\"MarketingArea\":\"CXXGLOBAL\"}," +
                      "\"__metadata\":{\"type\":\"CUAN_CONTACT_SRV.InteractionContact\"}," +
                      "\"FirstName\":\"SAP Diablo\",\"LastName\":\"SAP Wang\",\"Country\":\"CN\"," +
                      "\"EMailAddress\":\"seya@sap.com\",\"YY1_WECHATID_MPS\":\"i042416\"," +
                      "\"YY1_FACEID_MPS\":\"d042416\"}";
		tool.run(body);
	}
}

上述代码里,我硬编码了一个Contact的姓为SAP Wang,名为SAP diablo,
执行之后, 打印出API消费成功的201代码:

硬编码的数据能够在Marketing Cloud里观察到:

上述源代码在我的github上也能看到:https://github.com/i042416/JavaTwoPlusTwoEquals5/blob/master/src/partner1/SimpleContactCreator.java
要获取更多Jerry的原创文章,请关注公众号"汪子熙":

如何用Java代码在SAP Marketing Cloud里创建contact数据的更多相关文章
- SAP Marketing Cloud里的contact main facet是什么意思
		界面如下: Basically, contact data for SAP Hybris Marketing can be loaded from various sources, such as a ... 
- 如何在Marketing Cloud里创建extension field扩展字段
		首先在Marketing Cloud里找到创建扩展字段的tile入口,搜索关键字extension: 这会进入Fiori应用"Custom fields",能看到系统里所有创建好的 ... 
- 使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息
		我们先来看看Marketing Cloud系统里的contact信息: 一共1218374条数据. 我们用如下的nodejs代码通过OData来获取这些数据: var request = requir ... 
- SAP Marketing Cloud功能简述(四) : 线索和客户管理
		这个系列的前三篇文章Grace Dong已经给大家带来了下面的内容: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Clou ... 
- SAP Marketing Cloud功能简述(三)  营销活动内容设计和产品推荐
		Grace的前两篇文章: SAP Marketing Cloud功能简述(一) : Contacts和Profiles SAP Marketing Cloud功能简述(二) : Target Grou ... 
- SAP Marketing Cloud功能简述(二) : Target Group
		这个系列的第一篇文章 SAP Marketing Cloud功能简述(一) : Contacts和Profiles,我向大家介绍了SAP Marketing Cloud里的Contacts和Profi ... 
- SAP Marketing Cloud功能简述(一) : Contacts和Profiles
		Dong Grace是Jerry的同事,目前是SAP成都研究院数字创新空间团队的一名实习生,明年7月才毕业.这篇文章是Grace对SAP Marketing Cloud功能梳理的一篇学习笔记,因为篇幅 ... 
- SAP Marketing Cloud的Contact导入配置和数据合并原理
		SAP很多系统的主数据都支持从外部系统导入,SAP Marketing Cloud也是如此,contact主数据可以来自Hybris Commerce,CRM,ERP或者Twitter,Faceboo ... 
- 使用postman修改SAP Marketing Cloud contact主数据
		Marketing Cloud里的contact主数据,创建成功后也不是所有字段都能够被修改.在Personal data区域的字段是可以被修改的. 比如我在"客户属性"字段里维护 ... 
随机推荐
- features its own
			Gulp.js features its own built-in watch() method - no external plugin required ---- However, the Arn ... 
- Windows 10 搭建Python3 安装使用 protobuf
			Protobuf对比XML.Json等其他序列化的优势 protobuf 不管是处理时间上,还是空间占用上都优于现有的其他序列化方式.内存暂用是java 序列化的1/9,时间也是差了一个数量级,一次操 ... 
- java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
			在使用Spring框架时 报错 :java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource 原因:为引入spring-a ... 
- unity 读取灰度图生成三维地形并贴图卫星影像
			从 https://earthexplorer.usgs.gov/ 下载高程数据 从谷歌地球上保存对应地区卫星图像 从灰度图创建地形模型,并将卫星影像作为贴图 using System.Collect ... 
- 日常小节----unity小坑记(射线检测固定层级)
			unity中射线检测需设定所需层级时,必须加上距离!!! //一条从主相机到屏幕点击点的射线 Ray ray = Camera.Main.ScreenPointToRay(Input.mousePos ... 
- php display_errors
			// 检测开发环境 public function setReporting() { if (APP_DEBUG === true) { error_reporting(E_ALL); ini_set ... 
- linux netfilter
			yum -y install iptables//三张表 filter nat mangle [root@wang /]# iptables -t filter -nvL [root@wang /]# ... 
- Python时间日期格式化之time与datetime模块
			1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ... 
- 第一次git到GitHub过程
			首先在GitHub上创建一个仓库 
- HDU3191 【输出次短路条数】
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3191 How Many Paths Are There Time Limit: 2000/1000 M ... 
