前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何用Java代码在SAP Marketing Cloud里创建contact数据

如何用Java代码在SAP Marketing Cloud里创建contact数据

作者头像
Jerry Wang
发布2019-05-29 20:45:22
3520
发布2019-05-29 20:45:22
举报

版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1438543

我们可以使用SAP Marketing Cloud提供的Contact create OData API在第三方应用里创建Contact主数据.

API地址:/sap/opu/odata/sap/CUAN_CONTACT_SRV/InteractionContacts

示例代码只有100多行:

代码语言:javascript
复制
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里观察到:

源代码:https://github.com/i042416/JavaTwoPlusTwoEquals5/blob/master/src/partner1/SimpleContactCreator.java

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年05月25日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档