使用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=
# not batch
contactcreateurl=
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2688376/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 如何用Java程式碼在SAP Marketing Cloud裡建立contact資料JavaCloud
- 使用nodejs建立Marketing Cloud的contact資料NodeJSCloud
- 使用postman修改SAP Marketing Cloud contact主資料PostmanCloud
- 使用postman建立Marketing Cloud的ContactPostmanCloud
- SAP Marketing Cloud的Contact匯入配置和資料合併原理Cloud
- SAP Marketing Cloud Contact 模型的匯入配置和資料合併原理Cloud模型
- Marketing Cloud contact主資料的csv匯入Cloud
- 使用nodejs對Marketing Cloud的contact主資料進行修改操作NodeJSCloud
- SAP Marketing Cloud裡的contact main facet是什麼意思CloudAI
- Marketing Cloud的contact merge機制Cloud
- 使用nodejs實現OData的batch操作在Marketing Cloud裡讀取contact資訊NodeJSBATCloud
- 如何使用Chrome找到Marketing Cloud某個contact的guidChromeCloudGUI
- 使用CDS view開發SAP Marketing contact的facet追溯工具View
- 如何讓某些使用者對Marketing Cloud的contact資料只能實施只讀操作Cloud
- SAP Marketing Cloud Restful API SDK 使用案例分享CloudRESTAPI
- SAP Marketing Cloud 功能概述(一)Cloud
- SAP Marketing Cloud 功能概述(二)Cloud
- SAP Marketing Cloud 功能概述(三)Cloud
- SAP Marketing Cloud 功能概述(四)Cloud
- 如何在呼叫Marketing Cloud contact建立API時增加對擴充套件欄位的支援CloudAPI套件
- SAP Marketing Cloud功能簡述(二) : Target GroupCloud
- 在Google Cloud platform上建立Kubernetes cluster並使用GoCloudPlatform
- 在 SAP BTP 平臺 Neo 環境裡使用 SAP Cloud SDK 建立應用Cloud
- SAP Marketing Cloud功能簡述(一) : Contacts和ProfilesCloud
- 使用Gardener在Google Cloud Platform上建立Kubernetes叢集GoCloudPlatform
- 使用簡單的Java程式碼在SAP C4C裡建立銷售訂單Java
- 如何使用程式碼建立和讀取 SAP CRM 訂單的 Text 資料
- 使用nodejs程式碼在SAP C4C裡建立Individual customerNodeJS
- 如何使用ABAP程式碼建立SAP Product CategoryGo
- SAP HANA Cloud 學習教程之一: 如何在 SAP BTP 上建立 SAP HANA Cloud 例項Cloud
- 如何使用SAP CRM Marketing Survey建立一個市場問卷調查
- SAP Marketing Cloud功能簡述(五) 銷售計劃管理Cloud
- 在 SAP BTP 上體驗 SAP HANA Cloud 試用版本Cloud
- SAP HANA Cloud 學習教程之二: 如何往SAP BTP 上 HANA Cloud 資料庫表裡插入資料Cloud資料庫
- SAP Marketing Cloud功能簡述(四) : 線索和客戶管理Cloud
- 在SAP HANA Express Edition裡建立資料庫表Express資料庫
- 如何使用SAP Cloud Application Studio建立一個PDF formCloudAPPORM
- 如何在Marketing Cloud裡建立extension field擴充套件欄位Cloud套件