Salesforce

Salesforce Marketing Cloud 集成解决方

2020-09-24  本文已影响0人  Salesforce开发者

Salesforce Marketing Cloud提供了2种API集成解决方案:REST and SOAP APIs
本文简单介绍一下使用REST API教程。
更多详细信息可以查看官方文档:
https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-developer-basics
https://trailhead.salesforce.com/content/learn/modules/marketing-cloud-apis

我们打开Marketing Cloud Setup页面,找到Apps下面的Installed Packages,点击New按钮进行新建。

我们输入Package Name:Integration,点击Save保存。

我们点击Add Component按钮添加需要访问的组件,第一次添加时,需要选择Integration Type。

有三种类型可供选择:

它们之间的区别和详细内容可以查看官方文档:https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/integration-s2s-client-credentials.htm,在这里我们选择Server-to-Server。

确保该按钮已启用,可以Access。

打开Postman或谷歌Http接口调试插件,将请求地址和client_id、client_secret、account_id(就是MID)替换成你自己Marketing Cloud的。

Postman请求示例:

Host: https://YOUR_SUBDOMAIN.auth.marketingcloudapis.com
POST /v2/token
Content-Type: application/json

{
    "grant_type": "client_credentials",
    "client_id": "7a9j47upktedde30uedl822p",
    "client_secret": "1955278925675241571",
    "scope": "email_read email_write email_send",
    "account_id": "12345"
}

在此示例中,作用域反映了对API请求启用的权限,并且帐户ID是帐户的MID。

Apex代码请求示例:

public static String getAccessToken(){
        String accessToken = '';
        Http http = new Http();
        HttpRequest req = new HttpRequest();  
        req.setMethod('POST');
        req.setTimeout(12000);
        req.setHeader('Content-type','application/json');
        req.setEndpoint('https://YOUR_SUBDOMAIN.auth.marketingcloudapis.com/v2/token');
        String reqBody = JSON.serialize( new Map<String, String>{
                                            'grant_type' => 'client_credentials',
                                            'client_id' => 'your_client_id',
                                            'client_secret' => 'your_client_secret',
                                            'account_id' => 'your_account_id'
                                        } );
        System.debug(LoggingLevel.INFO, '*** reqBody: ' + reqBody);
        req.setBody(reqBody);
        HttpResponse response = http.send(req);
        System.debug(LoggingLevel.INFO, '*** response: ' + response);
        if (response.getStatusCode() == 200) {
            Map<String, Object> result = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            System.debug(LoggingLevel.INFO, '*** result: ' + result);
            accessToken = String.valueOf(result.get('access_token'));
        }
        return accessToken;
}

access_token的有效时间大概是1080秒。

请求成功:

{ 
"access_token":"eyJhbLciOiJIPzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjIifQ.eyJhY2Nlc3NfdG9rZW4iOiJhYmJUQTlpSHZqRjkyd3Jkb0xWZEFCaloiLCJjbGllbnRfaWQiOiI3ZTRmYW1xaWUzcWtzdzlhNDRrcmxvZDgiLCJlaWQiOjEwNzU3Njc2LCJzdGFja19rZXkiOiJRQTFTMSJ9.wSFfEdeNrkoiU_tnmJ2ihm8iUqnJKlZoI3GlavTGBhs.hU4EsiC1e9txh_TCt90YlI2l7xZZ5E6_oa0xku3Jj9CCk1B72M4bhO3kUIyhwfVuB0MFbL0y9KD_RRFzg-nuqPgjPyONnby-iWopdZPBHd-3woupxCMST5-vfJO9qAED9qiUfYLS4WmHRuJTCX4NPScyu8BdROTVEe-D3iAoAeFoJX_rLZ9d5eEhIn1AvkYgoj9siuxAprHEvmySTgNIXkQA6uT_IQ-H1dbfOyJmlFKpYzvhvHb0KH7NJ24zy5bd2MQ5",
"expires_in":1080,
"token_type":"Bearer",
"rest_instance_url":"mc563885gzs27c5t9-63k636tzgm.rest.marketingcloudapis.com",
"soap_instance_url":"mc563885gzs27c5t9-63k636tzgm.soap.marketingcloudapis.com",
"scope": "email_read email_write email_send"
}

我们将获取的access_token用来创建一个Contact:

public static String createContact(){
        Http http = new Http();
        HttpRequest req = new HttpRequest();  
        req.setMethod('POST');
        req.setTimeout(12000);
        req.setHeader('Content-type','application/json');
        req.setHeader('Authorization','Bearer '+getAccessToken());
        req.setEndpoint('https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/contacts/v1/contacts');
        String reqBody = '{"contactKey": "12345@sina.cn", "attributeSets": [{"name": "Email Addresses", "items": [{"values": [{"name": "Email Address", "value": "12345@sina.cn"}, {"name": "HTML Enabled", "value": true}]}]}]}';
        System.debug(LoggingLevel.INFO, '*** reqBody: ' + reqBody);
        req.setBody(reqBody);
        HttpResponse response = http.send(req);
        System.debug(LoggingLevel.INFO, '*** response.getBody(): ' + response.getBody());
        return String.valueOf(response.getBody());
}

请求成功结果:

{
    "requestServiceMessageID": "1a6732e1-f7ab-4785-a99a-b284104a86fa", 
    "contactKey": "12345@sina.cn", 
    "hasErrors": false, 
    "contactTypeID": 0, 
    "serviceMessageID": "324ce896-fba3-4bbd-a6f7-2670700ec451", 
    "responseDateTime": "2020-09-24T04:31:20.5276123-06:00", 
    "isNewContactKey": true, 
    "rowsAffected": 1, 
    "resultMessages": [], 
    "operationStatus": "OK", 
    "contactID": 39604926
}

以上就是使用REST API的简单教程,关于Salesforce Marketing Cloud REST API的更多教程后续会介绍。

上一篇下一篇

猜你喜欢

热点阅读