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

栏目: Java · 发布时间: 5年前

内容简介:我们可以使用SAP Marketing Cloud提供的Contact create OData API在第三方应用里创建Contact主数据.API地址:/sap/opu/odata/sap/CUAN_CONTACT_SRV/InteractionContacts示例代码只有100多行:

我们可以使用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);
    }
}

如何用 <a href='https://www.codercto.com/topics/22013.html'>Java</a> 代码在SAP Marketing Cloud里创建contact数据

上述代码里,我硬编码了一个Contact的姓为SAP Wang,名为SAP diablo,

执行之后, 打印出API消费成功的201代码:

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

硬编码的数据能够在Marketing Cloud里观察到:

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

上述源代码在我的github上也能看到: https://github.com/i042416/Ja...

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

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


以上所述就是小编给大家介绍的《如何用Java代码在SAP Marketing Cloud里创建contact数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

来自圣经的证明

来自圣经的证明

M.Aigner、G.M.Ziegler / 世界图书出版公司 / 2006-7 / 39.00元

作为一门历史悠久的学问,数学有她自身的文化和美学,就像文学和艺术一样。一方面,数学家们在努力开拓新领域、解决老问题;另一方面他们也在不断地从不同的角度反复学习、理解和欣赏前辈们的工作。的确,数学中有许多不仅值得反复推敲理解,更值得细心品味和欣赏的杰作。有些定理的证明不仅想法奇特、构思精巧,作为一个整体更是天衣无缝。难怪,西方有些虔诚的数学家将这类杰作比喻为上帝的创造。 本书已被译成8种文字。......一起来看看 《来自圣经的证明》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具