API を使い始める

このドキュメントでは、Google 入札管理 API を使用するアプリの作成を開始する方法について説明します。API を使用すると クエリと取得 レポートのメタデータ。

利用可能な最新バージョンは Bid Manager API v2 で、推奨されるバージョンです。

Google ディスプレイ&ビデオ 360 のコンセプトに慣れていない場合は、ディスプレイ&ビデオ 360 ヘルプセンターを参照し、UI を試してみてください。

2. 認証の準備をする

Bid Manager API を使用するには、まず設定ツールを使用して Google API Console でプロジェクトを作成し、API を有効にして認証情報を作成する必要があります。

まだ OAuth 2.0 の認証情報を作成していない場合は、[認証情報を作成] > [OAuth クライアント ID] の順にクリックして作成します。認証情報を作成すると、[認証情報] ページにクライアント ID が表示されます。[ クライアント シークレット、リダイレクト URI、JavaScript などの詳細のクライアント ID 送信元アドレス、メールアドレスなどがあります

詳細については、リクエストを承認するをご覧ください。

3. Bid Manager API を呼び出す

以下のタブには、さまざまな言語でコーディングするためのクイックスタートが用意されています。同様のサンプルコードは、Bid Manager API の例リポジトリでも確認できます。

JavaPythonPHP
  1. 必要なライブラリをインポートします。

    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;
  2. クライアントのシークレット ファイルを読み込み、認証情報を生成する

    この手順を初めて行うときは、信用照会に同意するよう求められます。 プロンプトが表示されます。承認する前に、ディスプレイ&ビデオ 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");
  3. 認証済みの API クライアントを作成する。

    // Create authorized API client.
    DoubleClickBidManager service =
        new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("bidmanager-java-installed-app-sample")
            .build();
  4. 操作を実行する

    // 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 ファイルを参照してください。

  1. 必要なライブラリをインポートします。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
  2. クライアントのシークレット ファイルを読み込み、認証情報を生成する

    この手順を初めて行うときは、信用照会に同意するよう求められます。 プロンプトが表示されます。承認する前に、ディスプレイ&ビデオ 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()
  3. 認証済みの 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)
  4. オペレーションを実行する。

    # 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 を実行し、関連するウェブページにリダイレクトするように認証情報を構成していることを前提としています。たとえば、index.php ファイル内のこのコードは、次のコマンドと、認証後に http://localhost:8000 にリダイレクトするように構成された認証情報を使用して実行できます。

php -S localhost:8000 -t ./

  1. Google API PHP クライアントをダウンロードしてインストールします。

    推奨される方法は Composer を使用する方法です。

    composer require google/apiclient:^2.12.1

    インストールが完了したら、必ずオートローダーを

    require_once '/path/to/your-project/vendor/autoload.php';

  2. Google_Client オブジェクトを作成します。

    $client = new Google_Client();
  3. クライアントを設定し、必要に応じて認証 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);
  4. ディスプレイ&ビデオ 360 API サービスのクライアントを作成します。

    $service = new Google_Service_DoubleClickBidManager($client);
  5. 操作を実行する

    // 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. 次のステップ

これでクライアント ライブラリを起動できたので、次はリファレンス 実装の詳細をご覧ください。

その他のガイドは、 スケジュール設定されたレポートの活用 および レポートのベスト プラクティスを実践する