专栏首页Jerry的SAP技术分享使用Java代码在SAP Marketing Cloud上创建Contact数据

使用Java代码在SAP Marketing Cloud上创建Contact数据

源代码:

package partner1;

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;

	public SimpleContactCreator(){
		enableHeaderWireAndContextLogging();
	}
	
	private void enableHeaderWireAndContextLogging(){
		System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
		System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire.header", "debug");
		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
		System.setProperty("org.apache.commons.logging.simplelog.defaultlog", "debug");
	}
	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);
	}
}

package partner1;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigUtil {
	
	Properties prop; 
	
	public ConfigUtil(){
		InputStream input = null;
		prop = new Properties();
		
		String propFileName = "config.properties";

		input = getClass().getClassLoader().getResourceAsStream(propFileName);

		if (input != null) {
				try {
					prop.load(input);
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	
	public String getConfig(String name){
		return prop.getProperty(name);
	}
	
	public static void main(String[] argv){
		ConfigUtil tool = new ConfigUtil();
		System.out.println("User: " + tool.getConfig("user"));
	}
}

config.properties文件放在resources文件夹下:

user=mkt
password=MY

tokenurl=https://jerry.hybris.com/sap/opu/odata/sap/CUAN_COMMON_SRV/?sap-client=100
# not batch
contactcreateurl=https://jerry.hybris.com/sap/opu/odata/sap/CUAN_CONTACT_SRV/InteractionContacts?sap-client=100

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

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

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

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

    版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。 https://jerry.blog....

    Jerry Wang
  • 使用简单的Java代码在SAP C4C里创建销售订单

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

    版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。 https://jerry.blog....

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

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

    Jerry Wang
  • SpringBoot入门建站全系列(十二)Spring Security使用token做认证

    Spring 是一个非常流行和成功的 Java 应用开发框架。Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决...

    品茗IT
  • Spark读写HBase之使用Spark自带的API以及使用Bulk Load将大量数据导入HBase

    以上两个算子分别是基于Hadoop新版API和hadoop旧版API实现的,大部分代码都一样,需要注意的是新版API使用中Job类,旧版API使用JobConf...

    CoderJed
  • Spring Boot(十四)RabbitMQ延迟队列

    延迟队列的使用场景:1.未按时支付的订单,30分钟过期之后取消订单;2.给活跃度比较低的用户间隔N天之后推送消息,提高活跃度;3.过1分钟给新注册会员的用户,发...

    Java中文社群-磊哥
  • 自己写一个简单的MyBatis工具类代码+注释

  • 使用shiro安全管理

    dalaoyang

扫码关注云+社区

领取腾讯云代金券