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


相关文章
|
7天前
|
Java 测试技术 应用服务中间件
常见 Java 代码缺陷及规避方式(下)
常见 Java 代码缺陷及规避方式(下)
29 0
|
9天前
|
Java
Java中ReentrantLock释放锁代码解析
Java中ReentrantLock释放锁代码解析
25 8
|
12天前
|
前端开发 小程序 Java
uniapp上传图片 前端以及java后端代码实现
uniapp上传图片 前端以及java后端代码实现
28 0
|
7天前
|
Java
代码的魔法师:Java反射工厂模式详解
代码的魔法师:Java反射工厂模式详解
20 0
|
7天前
|
监控 安全 Java
常见 Java 代码缺陷及规避方式(中)
常见 Java 代码缺陷及规避方式(中)
22 1
|
10天前
|
设计模式 算法 Java
23种设计模式,模板方法模式的概念优缺点以及JAVA代码举例
【4月更文挑战第10天】模板方法模式是一种行为设计模式,它定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些特定步骤。
13 0
|
11天前
|
设计模式 Java
23种设计模式,状态模式的概念优缺点以及JAVA代码举例
【4月更文挑战第9天】状态模式是一种行为设计模式,允许一个对象在其内部状态改变时改变它的行为,这个对象看起来似乎修改了它的类。
25 4
|
11天前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出"验证成功",否则输出"验证失败"。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
12天前
|
设计模式 Java
23种设计模式,命令模式的概念优缺点以及JAVA代码举例
【4月更文挑战第7天】命令模式是一种行为设计模式,它将请求或简单操作封装为一个对象。这种模式允许用户通过调用对象来参数化其他对象的方法,并能保存、排队和执行方法调用。
18 1
|
12天前
|
安全 UED 开发者
SAP Commerce Cloud 配置项 refreshWithLock=false 的作用介绍
SAP Commerce Cloud 配置项 refreshWithLock=false 的作用介绍
17 0