8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cURLコマンドでMarketing Cloud APIを使用してSalesforce Marketing Cloud (SMC)のデータエクステンションにレコードを追加する

Last updated at Posted at 2019-01-14

はじめに

Salesforceにはメールマーケティング用のユーザ向けメール配信等のマーケティング系機能を備えた製品 Salesforce Marketing Cloud があります。SMCという略称で呼ばれていたりもします。

Marketing Cloudでは データエクステンション という独自のテーブルを作成する事ができます。DEという略称で呼ばれていたりもします。

Marketing Cloudにはデータエクステンションに対するデータ登録等の様々な操作を行えるMarketing Cloud API (旧名: ExactTarget API) というAPIが提供されています。

本記事では、cURLコマンドを使って、Marketing Cloud APIを呼び出し、Marketing Cloudのデータエクステンションに新規レコードを追加するコマンドを記します。

実行環境

本記事のcURLを実行している環境は以下の通りです。

OS マシンの種類 OSバージョン カーネルバージョン
Mac MacBook PRO (Retina) macOS Mojave 10.14.1 Darwin Kernel Version 18.2.0

参考資料

Salesforce社様のMarketing Cloud APIリファレンスを参考にさせて頂きました。ありがとうございました。

Marketing Cloud API リファレンス -> POST /hub/v1/dataevents/key:{key}/rowset

データ登録対象のMarketing Cloudデータエクステンション名の定義例

今回、Marketing Cloud APIでレコードを追加するデータエクステンション例は以下の通りです。

| データエクステンション名 | example_smc_de_table |
|:------------|:------------|:------------|

カラム名 データ型  制約
id number型 プライマリキー
name string型 制約なし
email string型 制約なし

cURLコマンドでMarketing Cloudデータエクステンションにレコードを追加する例

(1) cURLコマンドでMarketing Cloudデータエクステンションにレコードを追加するスクリプトを作成します。

今回使用するMarketing Cloud APIは以下になります。
https://www.exacttargetapis.com/hub/v1/dataevents/key:{key}/rowset

以下のようなスクリプトを作成します。

$ vi insert_smc_de_rowset.sh

書き捨てなので、今回の例では、APIキーやシークレットは環境変数ではなくスクリプト内に書いています。
(運用で使用するなら、APIキーやシークレットは環境変数化して暗号化なりしておくと良いかもしれませんね)

insert_smc_de_rowset.sh
# !/bin/sh

##### Marketing Cloud APIのアクセストークン取得用API情報
## Marketing Cloud APIのOAuthアクセストークン取得用APIエンドポイント
export SMC_LOGIN_API_ENDPOINT="auth.exacttargetapis.com"
export SMC_LOGIN_API_VERSION="v1"
export SMC_LOGIN_API_URL="https://${SMC_LOGIN_API_ENDPOINT}/${SMC_LOGIN_API_VERSION}/requestToken"

##### Marketing Cloud APIアクセスキーとシークレットキー設定
## Marketing Cloud APIキー文字列をセットする
export SMC_LOGIN_API_KEY="*****************************"

## Marketing Cloud APIシークレット文字列をセットする
export SMC_LOGIN_API_SECRET="*********************************"

##### Marketing Cloud APIデータエクステンション操作用API情報
## Marketing Cloudデータエクステンション操作用APIエンドポイント
export SMC_DE_API_ENDPOINT="www.exacttargetapis.com"
export SMC_DE_API_VERSION="v1"

## データ登録対象のデータエクステンション名を記述する
export SMC_DE_KEY_NAME="example_smc_de_table"

## Marketing Cloudデータエクステンション操作用APIのURL
export SMC_DE_API_URL="https://${SMC_DE_API_ENDPOINT}/hub/${SMC_DE_API_VERSION}/dataeventsasync/key:${SMC_DE_KEY_NAME}/rowset"

##### Marketing Cloud APIアクセストークン取得
export SMC_LOGIN_API_ACCESS_TOKEN=`curl -s -X POST ${SMC_LOGIN_API_URL} -H "Content-Type: application/json" -d '{ "clientId": "'${SMC_LOGIN_API_KEY}'", "clientSecret": "'${SMC_LOGIN_API_SECRET}'" }' | awk 'BEGIN{FS=":"}{print $2}' | awk 'BEGIN{FS=","}{print $1}' | sed -e 's/"//g'`

##### Marketing Cloudデータエクステンションへ新規レコードを2件追加する
curl -s -X POST ${SMC_DE_API_URL} \
-H "Content-Type: application/json" \
-H "Authorization:Bearer ${SMC_LOGIN_API_ACCESS_TOKEN}" \
-d '[
  {
    "keys": {
      "id": "1"
      },
    "values": {
      "name": "hoge1",
      "email": "hoge1@example.com"
    }
  },
  {
    "keys": {
      "id": "2"
    },
    "values": {
      "name": "hoge2",
      "email": "hoge2@example.com"
    }
  }
]'

作成したシェルスクリプトの構文チェックを行います。以下のように何も表示されなければ、構文上の問題はございません。

$ sh -n insert_smc_de_rowset.sh
$ 

作成したシェルスクリプトに実行権限を付与します。

$ chmod 750 insert_smc_de_rowset.sh
$ 

(2) 前述で作成したシェルスクリプトを実行し、Marketing Cloudデータエクステンションにレコードを追加します。

スクリプトを実行すると、以下のように表示されます。
前述のcurlのように-vオプションなしで実行すると、実行結果は以下のように表示されます。

$ ./insert_smc_de_rowset.sh
""$

これでMarketing Cloudデータエクステンション「example_smc_de_table」に新規レコードが2件追加されます。

最後に

cURLコマンドにより、Salesforce Marketing Cloud APIも実行可能です。


以上になります。

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?