日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/19 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何用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数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。