使用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
本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
浅谈Java和SAP ABAP的静态代理和动态代理,以及ABAP面向切面编程的尝试
文章目录 Java的静态代理 静态代理的优缺点 ABAP的静态代理 Spring AOP的动态代理 JDK动态代理的优缺点 CGLIB动态代理的优缺点 ABAP CGLIB的模拟实现 ABAP Pre和Post Exit Jerry之前一篇文章 SAP产品增强技术回顾,提到基于Java编程语言实现的SAP Commerce,借助Spring框架的支持,能使用面向切面编程的理念(Aspect Orient Programming,以下简称AOP),将业务代码和非业务代码(比如权限检查,日志记录,性能统计等)彻底分离开。 下图是某应用里方法的常规实现:权限检查,日志记录和性能检测的代码一次又一次地侵入到本应只包含业务代码的三个方法中: 下图是应用AOP之后的方法实现:三个方法体内只包含纯粹的业务代码,看起来清爽了很多。权限检查,日志记录和性能检测的代码,作为仍需关注的三个方面,以切面的方式编织到三个方法中。Weave,AOP里的术语,中文材料里经常译成“编织”,描述了被代理类的方法通过非源代码修改层面被增添以新逻辑的动作。 我们说面向对象编程(Object Oriented Program...
- 下一篇
ABAP应用服务器的HTTP响应状态码(Status Code)
最近Jerry参与了SAP Commerce Cloud的标准开发,我们调用微软云平台Azure上创建Lambda Function的Restful API来创建Lambda Function: 在开发过程中发现该API工作不太稳定,同样的输入,时不时会返回HTTP 400 Bad Request:Encountered an error (InternalServerError) from host runtime 这个错误并不是总能重现。 通过排查,最后我们确认这个问题和我们调用API的代码无关,于是给Azure报了一个bug: 在分析定位问题时,不由得让我怀念起以前在ABAP On-Premise上做开发的一个便利之处——大多数问题都可以通过在ABAP应用服务器端调试来找到根源。 本文记录了2016年时,SAP成都研究院CRM开发团队在开发SAP CRM Fiori应用时的一些技术讨论,关于HTTP请求的响应状态码的差异。 当时我们用Chrome打开SAP Fiori应用,在Chrome开发者工具的network标签里,观察到有的请求响应码为HTTP 200,有的却是HTTP 3...
相关文章
文章评论
共有0条评论来说两句吧...