スタートガイド

キャンペーン マネージャー 360 API を使用すると、キャンペーン マネージャー 360 アカウントの情報にプログラムでアクセスできます。キャンペーン マネージャー 360 とレポート ビルダーのウェブサービスと同様に、キャンペーンとレポートの管理と作成に使用されます。

このガイドでは、Campaign Manager 360 API の使用を開始する方法を説明します。

Prerequisites

キャンペーン マネージャー 360 API を使用するには、次の前提条件を満たす必要があります。

  1. キャンペーン マネージャー 360 アカウントが必要です。登録については、広告主/代理店をご覧ください。

  2. キャンペーン マネージャー 360 アカウントで API アクセスを有効にする必要があります。ほとんどのアカウントではデフォルトで有効になっています。よくわからない場合は、アカウント担当者またはキャンペーン マネージャー 360 サポートチームにお問い合わせください。

  3. このアカウントにアクセスできるユーザー プロフィールが必要です。キャンペーン マネージャー 360 のアカウント管理者に、このアカウントに関連付けられているユーザー プロフィールの作成を依頼してください。

  4. キャンペーン マネージャー 360 の管理画面でユーザー プロフィールの権限を確認します。これらは、ユーザー プロフィールが API からアクセスできる内容を制御します。個別の API 権限はありません。

プロジェクトを作成する

キャンペーン マネージャー 360 API を使用するには、まず Google API Console でプロジェクトを作成または選択し、API を有効にする必要があります。このリンクを使用すると、手順に沿って キャンペーン マネージャー 360 API を自動的に有効にできます。

認証情報を生成する

キャンペーン マネージャー 360 API へのすべてのリクエストには、承認が必要です。認可の概要については、アプリケーションを Google に認可して識別するをご覧ください。

次の手順では、インストール済みアプリケーション フローで使用する OAuth 2.0 クライアント ID を作成するプロセスについて説明します。サービス アカウント フローで使用する認証情報を生成する手順については、サービス アカウントのガイドをご覧ください。

  1. 手順に沿って Google API Console プロジェクトを構成します

  2. API コンソールで [認証情報] ページを開きます。
  3. [認証情報を作成] > [OAuth クライアント ID] をクリックします。

    1. このプロジェクトの OAuth 同意画面を以前に構成していない場合は、ここで構成します。[同意画面を構成] をクリックします。

    2. ユーザータイプを選択し、[作成] をクリックします。

    3. 最初のフォームに記入します。この設定は後で必要に応じて編集できます。完了したら、[保存] をクリックします。

    4. [認証情報] > [認証情報を作成] > [OAuth クライアント ID] に移動して続行します。

  4. アプリケーションの種類として [デスクトップ アプリ] を選択し、名前を指定して [作成] をクリックします。

完了すると、OAuth 2.0 クライアント ID とクライアント シークレットが表示されます。これらを JSON 形式でダウンロードして保存し、後で使用できます。

クライアント ライブラリをインストールする

Campaign Manager 360 API は HTTP と JSON をベースに構築されているため、標準の HTTP クライアントであれば、API にリクエストを送信してレスポンスを解析できます。

しかし、Google API クライアント ライブラリを利用すれば、言語が高度に統合され、セキュリティも強化されます。また、リクエストの承認がサポートされます。クライアント ライブラリは多くのプログラミング言語で利用できます。これを使用することにより、HTTP リクエストの設定やレスポンスの解析を手作業で行う必要がなくなります。

まず、開発に使用しているプログラミング言語を選択します。

C#

最新の .NET 用キャンペーン マネージャー 360 API クライアント ライブラリをインストールします。インストールの管理には NuGet を使用することをおすすめします。

NuGet Package Manager Console を開き、次のコマンドを実行します。

Install-Package Google.Apis.Dfareporting.v3_4

詳細

Java

最新の Java 用キャンペーン マネージャー 360 API クライアント ライブラリをインストールします。インストールの管理には Maven を使用することをおすすめします。

pom.xml ファイルに次の依存関係を追加します。

<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-dfareporting</artifactId>
  <version>v4-rev20220611-1.32.1</version>
  <exclusions>
    <exclusion>
      <groupId>com.google.guava</groupId>
      <artifactId>guava-jdk5</artifactId>
    </exclusion>
  </exclusions>
</dependency>

詳細

PHP

最新の PHP 用キャンペーン マネージャー 360 API クライアント ライブラリをインストールします。インストールの管理には Composer を使用することをおすすめします。

ターミナルを開いて、次のコマンドを実行します。

composer require google/apiclient

インストール済みのライブラリを最新バージョンに更新するには、次の操作を行います。

composer update google/apiclient

システムによっては、これらのコマンドの前に sudo を付ける必要があります。

詳細

Python

最新の Python 用キャンペーン マネージャー 360 API クライアント ライブラリをインストールします。インストールの管理には pip を使用することをおすすめします。

ターミナルを開いて、次のコマンドを実行します。

pip install --upgrade google-api-python-client

システムによっては、これらのコマンドの前に sudo を付ける必要があります。

詳細

Ruby

最新の Ruby 用のキャンペーン マネージャー 360 API クライアント ライブラリをインストールします。インストールの管理には RubyGems を使用することをおすすめします。

ターミナルを開いて、次のコマンドを実行します。

gem install google-api-client

インストール済みのライブラリを最新バージョンに更新するには、次の操作を行います。

gem update -y google-api-client

システムによっては、これらのコマンドの前に sudo を付ける必要があります。

詳細

サポートされている言語については、クライアント ライブラリ ページをご覧ください。

リクエストを作成する

OAuth 2.0 認証情報を作成し、クライアント ライブラリをインストールしたら、Campaign Manager 360 API を使用する準備は完了です。以下のクイックスタートに従って、承認、クライアントの構成、最初のリクエストを行う方法を学習します。

C#

  1. クライアント シークレット ファイルを読み込み、認証情報を生成する。

    この手順を初めて実行するときは、ブラウザで承認のプロンプトを表示するよう求められます。承認する前に、キャンペーン マネージャー 360 へのアクセス権を持つ Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリケーションがデータにアクセスすることが承認されます。

    // Load client secrets from the specified JSON file.
    GoogleClientSecrets clientSecrets;
    using(Stream json = new FileStream(pathToJsonFile, FileMode.Open, FileAccess.Read)) {
      clientSecrets = GoogleClientSecrets.Load(json);
    }
    
    // Create an asynchronous authorization task.
    //
    // Note: providing a data store allows auth credentials to be cached, so they survive multiple
    // runs of the application. This avoids prompting the user for authorization every time the
    // access token expires, by remembering the refresh token. The "user" value is used to
    // identify a specific set of credentials within the data store. You may provide different
    // values here to persist credentials for multiple users to the same data store.
    Task<UserCredential> authorizationTask = GoogleWebAuthorizationBroker.AuthorizeAsync(
        clientSecrets.Secrets,
        OAuthScopes,
        "user",
        CancellationToken.None,
        dataStore);
    
    // Authorize and persist credentials to the data store.
    UserCredential credential = authorizationTask.Result;
    
  2. Dfareporting の承認済みクライアントを作成します。

    // Create a Dfareporting service object.
    //
    // Note: application name should be replaced with a value that identifies your application.
    service = new DfareportingService(
        new BaseClientService.Initializer {
          HttpClientInitializer = credential,
          ApplicationName = "C# installed app sample"
        }
    );
    
  3. オペレーションを実行します。

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = service.UserProfiles.List().Execute();
    
    foreach (UserProfile profile in profiles.Items) {
      Console.WriteLine("Found user profile with ID {0} and name \"{1}\".",
          profile.ProfileId, profile.UserName);
    }
    

Java

  1. クライアント シークレット ファイルを読み込み、認証情報を生成する。

    この手順を初めて実行するときは、ブラウザで承認のプロンプトを表示するよう求められます。承認する前に、キャンペーン マネージャー 360 へのアクセス権を持つ Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリケーションがデータにアクセスすることが承認されます。

    // Load the client secrets JSON file.
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(
            jsonFactory, Files.newBufferedReader(Paths.get(pathToClientSecretsFile), UTF_8));
    
    // Set up the authorization code flow.
    //
    // Note: providing a DataStoreFactory allows auth credentials to be cached, so they survive
    // multiple runs of the program. This avoids prompting the user for authorization every time the
    // access token expires, by remembering the refresh token.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, clientSecrets, OAUTH_SCOPES)
            .setDataStoreFactory(dataStoreFactory)
            .build();
    
    // Authorize and persist credentials to the data store.
    //
    // Note: the "user" value below is used to identify a specific set of credentials in the data
    // store. You may provide different values here to persist credentials for multiple users to
    // the same data store.
    Credential credential =
        new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    
  2. Dfareporting の承認済みクライアントを作成します。

    // Create a Dfareporting client instance.
    //
    // Note: application name below should be replaced with a value that identifies your
    // application. Suggested format is "MyCompany-ProductName/Version.MinorVersion".
    Dfareporting reporting =
        new Dfareporting.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("dfareporting-java-installed-app-sample")
            .build();
    
  3. オペレーションを実行します。

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = reporting.userProfiles().list().execute();
    for (int i = 0; i < profiles.getItems().size(); i++) {
      System.out.printf("%d) %s%n", i + 1, profiles.getItems().get(i).getUserName());
    }
    

PHP

  1. クライアント シークレット ファイルを読み込み、認証情報を生成する。

    この手順を初めて実行するときは、ブラウザで承認のプロンプトを表示するよう求められます。承認する前に、キャンペーン マネージャー 360 へのアクセス権を持つ Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリケーションがデータにアクセスすることが承認されます。

    // Create a Google_Client instance.
    //
    // Note: application name should be replaced with a value that identifies
    // your application. Suggested format is "MyCompany-ProductName".
    $client = new Google_Client();
    $client->setAccessType('offline');
    $client->setApplicationName('PHP installed app sample');
    $client->setRedirectUri(self::OAUTH_REDIRECT_URI);
    $client->setScopes(self::$OAUTH_SCOPES);
    
    // Load the client secrets file.
    $client->setAuthConfig($pathToJsonFile);
    
    // Try to load cached credentials from the token store. Using a token store
    // allows auth credentials to be cached, so they survive multiple runs of
    // the application. This avoids prompting the user for authorization every
    // time the access token expires, by remembering the refresh token.
    if (file_exists($tokenStore) && filesize($tokenStore) > 0) {
        $client->setAccessToken(file_get_contents($tokenStore));
    } else {
        // If no cached credentials were found, authorize and persist
        // credentials to the token store.
        print 'Open this URL in your browser and authorize the application.';
        printf("\n\n%s\n\n", $client->createAuthUrl());
        print 'Enter the authorization code: ';
        $code = trim(fgets(STDIN));
        $client->authenticate($code);
    
        file_put_contents($tokenStore, json_encode($client->getAccessToken()));
    }
    
  2. Dfareporting の承認済みクライアントを作成します。

    // Create a Dfareporting service object.
    $service = new Google_Service_Dfareporting($client);
    
  3. オペレーションを実行します。

    // Retrieve and print all user profiles for the current authorized user.
    $result = $service->userProfiles->listUserProfiles();
    foreach ($result['items'] as $userProfile) {
        printf(
            "User profile \"%s\" (ID: %d) found for account %d.\n",
            $userProfile->getUserName(),
            $userProfile->getProfileId(),
            $userProfile->getAccountId()
        );
    }
    

Python

  1. クライアント シークレット ファイルを読み込み、認証情報を生成する。

    この手順を初めて実行するときは、ブラウザで承認のプロンプトを表示するよう求められます。承認する前に、キャンペーン マネージャー 360 へのアクセス権を持つ Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリケーションがデータにアクセスすることが承認されます。

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
        path_to_client_secrets_file, scope=OAUTH_SCOPES)
    
    # Check whether credentials exist in the credential store. Using a credential
    # store allows auth credentials to be cached, so they survive multiple runs
    # of the application. This avoids prompting the user for authorization every
    # time the access token expires, by remembering the refresh token.
    storage = Storage(CREDENTIAL_STORE_FILE)
    credentials = storage.get()
    
    # If no credentials were found, go through the authorization process and
    # persist credentials to the credential store.
    if credentials is None or credentials.invalid:
      credentials = tools.run_flow(flow, storage,
                                   tools.argparser.parse_known_args()[0])
    
    # Use the credentials to authorize an httplib2.Http instance.
    http = credentials.authorize(httplib2.Http())
    
  2. Dfareporting の承認済みクライアントを作成します。

    # Construct a service object via the discovery service.
    service = discovery.build('dfareporting', 'v4', http=http)
    
  3. オペレーションを実行します。

    # Construct the request.
    request = service.userProfiles().list()
    
    # Execute request and print response.
    response = request.execute()
    
    for profile in response['items']:
      print('Found user profile with ID %s and user name "%s".' %
            (profile['profileId'], profile['userName']))
    

Ruby

  1. クライアント シークレット ファイルを読み込み、認証情報を生成する。

    この手順を初めて実行するときは、ブラウザで承認のプロンプトを表示するよう求められます。承認する前に、キャンペーン マネージャー 360 へのアクセス権を持つ Google アカウントでログインしていることを確認してください。現在ログインしているアカウントに代わって、アプリケーションがデータにアクセスすることが承認されます。

    # Load client ID from the specified file.
    client_id = Google::Auth::ClientId.from_file(path_to_json_file)
    
    # Set up the user authorizer.
    #
    # Note: providing a token store allows auth credentials to be cached, so they
    # survive multiple runs of the application. This avoids prompting the user for
    # authorization every time the access token expires, by remembering the
    # refresh token.
    authorizer = Google::Auth::UserAuthorizer.new(
      client_id, [API_NAMESPACE::AUTH_DFAREPORTING], token_store
    )
    
    # Authorize and persist credentials to the data store.
    #
    # Note: the 'user' value below is used to identify a specific set of
    # credentials in the token store. You may provide different values here to
    # persist credentials for multiple users to the same token store.
    authorization = authorizer.get_credentials('user')
    if authorization.nil?
      puts format(
        "Open this URL in your browser and authorize the application.\n\n%s" \
        "\n\nEnter the authorization code:",
        authorizer.get_authorization_url(base_url: OAUTH_REDIRECT_URI)
      )
      code = STDIN.gets.chomp
      authorization = authorizer.get_and_store_credentials_from_code(
        base_url: OAUTH_REDIRECT_URI, code: code, user_id: 'user'
      )
    end
    
  2. Dfareporting の承認済みクライアントを作成します。

    # Create a Dfareporting service object.
    #
    # Note: application name should be replaced with a value that identifies
    # your application. Suggested format is "MyCompany-ProductName".
    service = API_NAMESPACE::DfareportingService.new
    service.authorization = authorization
    service.client_options.application_name = 'Ruby installed app sample'
    service.client_options.application_version = '1.0.0'
    
  3. オペレーションを実行します。

    // Retrieve and print all user profiles for the current authorized user.
    UserProfileList profiles = service.UserProfiles.List().Execute();
    
    foreach (UserProfile profile in profiles.Items) {
      Console.WriteLine("Found user profile with ID {0} and name \"{1}\".",
          profile.ProfileId, profile.UserName);
    }
    

その他の情報

API が提供するすべてのサービスについては、API リファレンスをご覧ください。すべてのメソッドの詳細ページには、ブラウザから直接テスト リクエストを行うための API Explorer が組み込まれています。

その他のガイドで、高度なトピックを取り上げ、一般的なタスクのエンドツーエンドの例をご確認ください。

コードを記述する準備が整ったら、Google の広範なコードサンプルをご覧ください。コードサンプルはニーズに合わせて変更できます。