# Audience StudioにおけるGoogle Ads V2 Export Integration CLI

## 'td'コマンドのインストール

最新の[TD Toolbelt](https://toolbelt.treasuredata.com/)をインストールしてください。

## Google OAuth2認証情報の作成

以下の手順に従って、OAuth認証情報(`client_id`および`client_secret`)を作成します:

1. Google API ConsoleのCredentialsを開きます: [https://console.developers.google.com/apis/credentials](https://console.developers.google.com/apis/credentials)
2. プロジェクトのドロップダウン(左上隅)から、**New Project**をクリックし、プロジェクトの名前を入力し、必要に応じて提供されたProject IDを編集します。**Create**をクリックします。


![](/assets/image-20191015-160905.caa8b1cce14fbbb9c01caa3b70963985095e89e53e2c9836292726eb4f5a5da7.7999db90.png)

1. Credentialsページで、Create credentialsタブから、**OAuth client ID**を選択します。
2. Consent screenでプロダクト名を設定するように求められる場合があります。その場合は、**Configure consent screen**を選択し、要求された情報を入力し、**Save**をクリックしてCredentials画面に戻ります。
3. Application Typeとして「Web Application」を選択します。OAuth認証情報の名前を入力します。必ず「Authorized redirect URIs」にGoogle OAuth playground URLを追加してください: [https://developers.google.com/oauthplayground](https://developers.google.com/oauthplayground)


![](/assets/image-20191015-160917.c527c0b2f63ed9eab48d2decc1395086c4920fc2292223e959db3a620f2a2fd0.7999db90.png)

1. **Create**を選択します。
2. client IDとclient secretをクリップボードにコピーします。コネクタジョブの設定時にこの情報が必要になります。


![](/assets/image-20191015-160925.b9931ce15fa920878c8811651e735d917c808063fa76ca0f24488604939e513b.7999db90.png)

詳細については、[Googleの認証情報に関する情報](https://developers.google.com/adwords/api/docs/guides/authentication#webapp)をご覧ください。

## Ads AccountをOAuth認証情報で承認

Google OAuth Playgroundを使用して、OAuth認証情報から`refresh_token`を生成できます。このプロセスは、Ads AccountにアクセスするためにOAuthアプリを承認するためのものです。

1. [https://developers.google.com/oauthplayground](https://developers.google.com/oauthplayground)にアクセスします
2. 右上隅の**Settings**を選択し、**Use your own OAuth credentials**にチェックを入れます。
3. 前のセクションから「OAuth Client ID」と「OAuth Client Secret」を入力します
4. 以下の必要なscopes(OAuth2パーミッション)を入力します:


- https://www.googleapis.com/auth/adwords
- https://www.googleapis.com/auth/userinfo.profile
- https://www.googleapis.com/auth/userinfo.email


![](/assets/image-20191015-160940.bc3fc4fddf91450a594060edc259a955458f58daee8f51806d515370c76a8b87.7999db90.png)

1. **Authorize APIs**を選択し、Google Ads Accountにログインします。
2. OAuth2 Playgroundにリダイレクトされるので、**Exchange authorization codes for tokens**を選択します。


![](/assets/image-20191015-160952.f30746ee731818a9d52d272f96540aaf935a9cd5296e0d9dcfad3ec212819438.7999db90.png)

## クエリコマンドの準備

`td query`コマンドの`-r` / `--result`オプションを使用して、Google Ads Remarketingの結果出力先を追加します:


```bash
td query -d mydb 'SELECT email FROM app_users' -r '{"type":"google_adwords_v2","client_id":"773#######################################j33.apps.googleusercontent.com","client_secret":"A######################N","developer_token":"9####################Q","refresh_token":"1/CG#######################################KI","client_customer_id":"111-222-3333","name":"abc_app_user_list","description":"User list from mobile app abc","app_id":"abc","mode":"append","membership_lifespan":10000,"batch_size":100000,"maximum_retries":5,"initial_retry_interval_millis":500,"maximum_retry_interval_millis":300000}'
```

各パラメータの説明:

- `client_id`: (必須) Google OAuth2認証情報
- `client_secret`: (必須) Google OAuth2認証情報
- `refresh_token`: (必須) Google OAuth2認証情報
- `developer_token`: (必須) Google Ads Developer Token
- `client_customer_id`: (必須) Google Ads Customer ID。形式: `nnn-nnn-nnnn` (nは数字)
- `name`: (必須) ユーザーリストの名前
- `description`: (オプション) ユーザーリストの説明。デフォルト: *空*
- `app_id`: モバイルアプリケーションID (`mobile_id`エクスポートの場合のみ必須)
- `mode`: (オプション) アップロードモード。デフォルト: append。サポートされる値:
  - `append`
  - `remove`
- `membership_lifespan`: (オプション) ユーザーの連絡先情報がユーザーリストに保持される日数。デフォルト: `10000`
- `batch_size`: (オプション) 各バッチでアップロードするレコード数。デフォルト: `100000`
- `maximum_retries`: (オプション) システムが諦めるまでのリトライ回数。デフォルト: `5`
- `initial_retry_interval_millis`: (オプション) 初回リトライ待機時間(ミリ秒)。デフォルト: `500`(0.5秒相当)
- `maximum_retry_interval_millis`: (オプション) 最大リトライ待機時間(ミリ秒)。デフォルト: `300000`(5分相当)