Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java code to create Contact data on SAP Marketing Cloud

Shulou Source: shulou.com Published: 2022-06-02 18:53:23 09月24日 Update

This article focuses on "how to use Java code to create Contact data on SAP Marketing Cloud", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use Java code to create Contact data on SAP Marketing Cloud.

Source code:

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 masked 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"));}}

The config.properties file is placed under the resources folder:

User=mktpassword=MYtokenurl= https://jerry.hybris.com/sap/opu/odata/sap/CUAN_COMMON_SRV/?sap-client=100# not batchcontactcreateurl= https://jerry.hybris.com/sap/opu/odata/sap/CUAN_CONTACT_SRV/InteractionContacts?sap-client=100 here, I believe you have a deeper understanding of "how to use Java code to create Contact data on SAP Marketing Cloud". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Code data content files learning practical deeper interest practical easy to operate folders methods more friends source code websites channels queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn macOS Shulou Tech Info Shulou Information MariaDB