このドキュメントでは、Google Bid Manager API を使ったアプリの作成を開始する方法について説明します。この API を使用すると、クエリを管理し、レポートのメタデータを取得できます。
Bid Manager API v2 が最新の推奨バージョンです。
1. 始める前に
Google ディスプレイ&ビデオ 360 のコンセプトに慣れていない場合は、ディスプレイ&ビデオ 360 ヘルプセンターをお読みになり、管理画面をお試しください。
2. 認証の準備をする
Bid Manager API を使用するには、まずセットアップ ツールを使用する必要があります。このツールを使って、Google API Console でのプロジェクトの作成、API の有効化、認証情報の作成を指示に沿って行うことができます。
OAuth 2.0 の認証情報をまだ作成していない場合は、[認証情報を作成] > [OAuth クライアント ID] をクリックして作成します。認証情報を作成すると、[認証情報] ページにクライアント ID が表示されます。クライアント ID をクリックすると、クライアント シークレット、リダイレクト URI、JavaScript のオリジン アドレス、メールアドレスなどの詳細が表示されます。詳細については、リクエストの承認をご覧ください。
3. Bid Manager API を呼び出す
以下のタブでは、さまざまな言語のコーディングのクイックスタートを紹介しています。同様のサンプルコードは、Bid Manager API の例のリポジトリにもあります。
Java
必要なライブラリをインポートします。
import static java.nio.charset.StandardCharsets.UTF_8; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.util.Utils; import com.google.api.services.doubleclickbidmanager.DoubleClickBidManager; import com.google.api.services.doubleclickbidmanager.model.ListQueriesResponse; import com.google.api.services.doubleclickbidmanager.model.Query; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths;
クライアント シークレット ファイルを読み込み、認証情報を生成します。
この手順を初めて行う際は、ブラウザで承認プロンプトを承認するよう求められます。承認する前に、ディスプレイ&ビデオ 360 にアクセスできる Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリへのデータアクセスが承認されます。
// Read client secrets file. GoogleClientSecrets clientSecrets; try (Reader reader = Files.newBufferedReader(Paths.get(path-to-client-secrets-file), UTF_8)) { clientSecrets = GoogleClientSecrets.load(Utils.getDefaultJsonFactory(), reader); } // Generate authorization credentials. // Set up the authorization code flow. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( Utils.getDefaultTransport(), Utils.getDefaultJsonFactory(), clientSecrets, oauth-scopes) .build(); Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
承認済みの API クライアントを作成します。
// Create authorized API client. DoubleClickBidManager service = new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("bidmanager-java-installed-app-sample") .build();
オペレーションを実行します。
// Perform an operation. // Call the API, getting a list of 10 queries. ListQueriesResponse queriesResponse = service.queries().list().setPageSize(10).execute(); // Print them out. System.out.println("Id\t\tName"); if (queriesResponse.getQueries().size() > 0) { for (int i = 0; i < queriesResponse.getQueries().size(); i++) { Query currentQuery = queriesResponse.getQueries().get(i); System.out.printf( "%s\t%s%n", currentQuery.getQueryId(), currentQuery.getMetadata().getTitle()); } } else { System.out.println("No queries exist."); }
Java で Bid Manager API を使用する方法について詳しくは、Bid Manager API の例の README ファイルをご覧ください。
Python
必要なライブラリをインポートします。
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
クライアント シークレット ファイルを読み込み、認証情報を生成します。
この手順を初めて行う際は、ブラウザで承認プロンプトを承認するよう求められます。承認する前に、ディスプレイ&ビデオ 360 にアクセスできる Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリへのデータアクセスが承認されます。
# Set up a flow object to create the credentials using the # client secrets file and OAuth scopes. credentials = InstalledAppFlow.from_client_secrets_file( path-to-client-secrets-file, oauth-scopes).run_local_server()
承認済みの API クライアントを作成します。
# Build the discovery document URL. discovery_url = f'https://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2' # Build the API service. service = discovery.build( 'doubleclickbidmanager', 'v2', discoveryServiceUrl=discovery_url, credentials=credentials)
オペレーションを実行します。
# Build and execute queries.listqueries request. response = service.queries().list(pageSize='10').execute() # Print queries out. if 'queries' in response: print('Id\t\tName') for query in response['queries']: print('%s\t%s' % (query['queryId'], query['metadata']['title'])) else: print('No queries exist.')
Python で Bid Manager API を使用する方法について詳しくは、Bid Manager API の例の README ファイルをご覧ください。
PHP
このサンプルでは、組み込みのウェブサーバーで PHP を実行し、関連するウェブページにリダイレクトするように認証情報が構成されていることを前提としています。たとえば、index.php
ファイル内のこのコードは、認証後に http://localhost:8000
にリダイレクトするように構成された次のコマンドと認証情報を使用して実行できます。
php -S localhost:8000 -t ./
Google API PHP クライアントをダウンロードしてインストールします。
おすすめの方法は、Composer を使用する方法です。
composer require google/apiclient:^2.12.1
インストールが完了したら、必ずオートローダー
require_once '/path/to/your-project/vendor/autoload.php';
を含めてください。Google_Client オブジェクトを作成します。
$client = new Google_Client();
クライアントを設定し、必要に応じて認証 URL にリダイレクトして、アクセス トークンを取得します。
この手順を初めて行う際は、ブラウザで承認プロンプトを承認するよう求められます。承認する前に、ディスプレイ&ビデオ 360 にアクセスできる Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリへのデータアクセスが承認されます。
// Set up the client. $client->setApplicationName('DBM API PHP Samples'); $client->addScope(oauth-scope); $client->setAccessType('offline'); $client->setAuthConfigFile(path-to-client-secrets-file); // If the code is passed, authenticate. If not, redirect to authentication page. if (isset($_GET['code'])) { $client->authenticate($_GET['code']); } else { $authUrl = $client->createAuthUrl(); header('Location: ' . $authUrl); } // Exchange authorization code for an access token. $accessToken = $client->getAccessToken(); $client->setAccessToken($accessToken);
Display & Video 360 API サービスのクライアントを作成します。
$service = new Google_Service_DoubleClickBidManager($client);
オペレーションを実行します。
// Configure params for the Queries.listqueries request. $optParams = array('pageSize' => 10); // Execute the request. $result = $service->queries->listQueries($optParams); // Print the retrieved queries. if (!empty($result->getQueries())) { print('<pre><p>Id Name</p>'); foreach ($result->getQueries() as $query) { printf('<p>%s %s</p>', $query->queryId, $query->metadata->title); } print('</pre>'); } else { print '<p>No queries exist.</p>'; }
PHP で Bid Manager API を使用する方法について詳しくは、Bid Manager API の例の README ファイルをご覧ください。
4. 次のステップ
クライアント ライブラリが稼働したので、リファレンス ドキュメントを参照して実装の構築を始めましょう。
また、スケジュール設定されたレポートの活用とレポートのベスト プラクティスの適用に関するその他のガイドもご利用になれます。