このガイドでは、Ads Data Hub REST API を使用して Ads Data Hub を操作するためのアプリケーションを作成する方法について説明します。Ads Data Hub REST API を使用すると、ご利用の Google アカウントに関連付けられている Ads Data Hub ユーザーを表示したり、クエリを作成して実行したりできます。
設定
Ads Data Hub API を使用する前に、いくつかの手順を済ませておく必要があります。
- API を有効にするユーザーに、Google Cloud プロジェクトの
serviceusage.services.enable権限が付与されていることを確認します。また、serviceusage.services.enable権限を持つユーザーは、API にアクセスできる許可リストに登録されている必要もあります。 - クライアントの認証情報またはサービス アカウントが作成された Google Cloud プロジェクトで、Ads Data Hub API を有効化します。コンソールを使用して Ads Data Hub API をプロジェクトで有効にする手順は次のとおりです。
- Cloud Console API ライブラリに移動します。
- リストの中から使用するプロジェクトを選択します。
- 「Ads Data Hub API」を検索します。
- [API] ページで、[有効にする] をクリックします。
- 権限を管理する手順は次のとおりです。
- 認証情報の作成に使用するメールアドレスまたはサービス アカウントを、適切な権限を付与して Ads Data Hub に追加する必要があります。サービス アカウントの場合は、サービス アカウントのメールアドレスを追加します。OAuth の場合、ユーザーのメールアドレスを追加します。これにより、サービス アカウントやエンドユーザーのアカウントに、Ads Data Hub でクエリを実行する権限が付与されます。
- (推奨)Google API クライアント ライブラリをインストールします。
- Google API クライアント ライブラリは、さまざまな一般的な言語で利用できます。また、多くの Google API に対応しています。これは必須ではありませんが、クライアント ライブラリを使用すれば、記述する必要があるコードの数が減り、認証の設定が簡単になります。
| クライアント ライブラリ | Ads Data Hub のサンプル |
|---|---|
| Java の Google API クライアント ライブラリ | Java |
| Python の Google API クライアント ライブラリ |
認証と認可
Ads Data Hub API は、Ads Data Hub のユーザー アカウントにアクセスしてデータを変更できるため、お客様ご自身が認証済みユーザーであることを確認する必要があります。そのため、Ads Data Hub API を利用する前に、認証フローを済ませておく必要があります。認証フローを済ませることで、API を利用するために必要な権限が付与されます。認証は、OAuth 2.0 またはサービス アカウントを使用して行うことができます。
OAuth 2.0 のセットアップ
API はインストール済みアプリケーションとウェブ アプリケーション フローの両方をサポートしていますが、この例では、インストール済みアプリケーションのフローを使用します。
- Google API Console に移動して、管理者プロジェクトに移動します。
- プロジェクトで Ads Data Hub API が有効になっていることを確認します。
- 有効になっていない場合は、[+ API とサービスの有効化] をクリックして有効にします。
- 左側のナビゲーションで [認証情報] をクリックします。
- [認証情報を作成] プルダウン メニューを開いて、[OAuth クライアント ID] を選択します。表示されたページで、次の操作を行います。
- [その他] を選択します。
- 必要に応じて、クライアントの名前を入力します。
- [作成] をクリックします。
- 作成した認証情報の横にあるダウンロード アイコンをクリックします。
API キーを取得する
API キー(デベロッパー キー)は、以下の Python コードサンプルのように、認証に使用されます。API キーを取得する手順は次のとおりです。
- API Console の [認証情報] ページに移動します。
- 上部のナビゲーションのプルダウンで管理者プロジェクトが選択されていることを確認します。
- API キーの横にあるダウンロード アイコンをクリックします。
サンプル リクエストを送信する
Python
"""This sample shows how to retrieve all accounts associated with the user. For the program to execute successfully, ensure that you run it using Python 3. """ import json from google_auth_oauthlib import flow from googleapiclient.discovery import build appflow = flow.InstalledAppFlow.from_client_secrets_file( # Replace client_secrets.json with your own client secret file. 'client_secrets.json', scopes=['https://www.googleapis.com/auth/adsdatahub']) appflow.run_local_server() credentials = appflow.credentials developer_key = input('Developer key: ').strip() # For versions of the Google API Python client library prior to 2.0, the # `static_discovery` parameter below should be removed. service = build('AdsDataHub', 'v1', credentials=credentials, developerKey=developer_key, static_discovery=False) def pprint(x): print(json.dumps(x, sort_keys=True, indent=4)) adh_account_id = input('ADH account ID (e.g. "customers/123456789"): ').strip() pprint(service.customers().analysisQueries().list(parent =adh_account_id).execute())
Java
/* * Copyright (c) 2019 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.google.api.services.samples.adsdatahub.cmdline; import com.google.api.services.adsdatahub.v1.AdsDataHub; import com.google.api.services.adsdatahub.v1.model.Customer; import com.google.api.services.adsdatahub.v1.model.ListCustomersResponse; import java.io.IOException; import java.util.List; /** * This sample illustrates how to retrieve all accounts associated to the user. * * <p>See the <a href="customers.list reference * documentation">https://developers.google.com/ads-data-hub/reference/rest/v1/customers/list</a> * for more details. */ public class ListCustomers extends BaseSample { @Override public String getName() { return "List customers"; } @Override public String getDescription() { return "Lists customers associated with the authorized user"; } @Override public void execute(AdsDataHub client) throws IOException { String pageToken = null; boolean noData = true; System.out.println("========================================"); System.out.printf("Listing of customers associated with the authorized user:\n"); System.out.println("========================================"); do { ListCustomersResponse response = client.customers().list().setPageToken(pageToken).execute(); pageToken = response.getNextPageToken(); List<Customer> customers = response.getCustomers(); if (customers != null && customers.size() > 0) { noData = false; for (Customer customer : customers) { System.out.printf("* Customer id: %d\n", customer.getCustomerId()); System.out.printf("\tCustomer name: %s\n", customer.getDisplayName()); } } } while (pageToken != null); if (noData) { System.out.println("No customers were found associated with the authorized user."); } } }
次のステップ
- Ads Data Hub REST API で作成して実行できるクエリの例については、Ads Data Hub のサンプルクエリをご覧ください。
- サンプルを展開して、API の仕組みをよく理解し、ユースケースに合わせてカスタマイズしてください。その後、以下を行います。
- クエリの処理ステータスを調べます。
- BigQuery クライアント ライブラリを使用して、完了したクエリの結果を取得します。
- API に関する質問やフィードバックがある場合は、ADH サポートにお問い合わせください。