開始使用這個 API

本文件將說明如何著手編寫使用 Google Bid Manager API。您可以透過這個 API 查詢和擷取 報表中繼資料。

Bid Manager API 第 2 版是最新的可用版本 版本。

1. 事前準備

如果您不熟悉 Google 多媒體廣告和請參閱影片廣告 360 概念 多媒體和Video 360 說明中心UI 進行實驗。

2. 為驗證做好準備

如要開始使用 Bid Manager API,首先請 使用 設定工具,會引導您在 Google API 控制台,啟用 API 並建立憑證。

如果您尚未建立 OAuth 2.0 憑證,請按一下 建立憑證 >OAuth 用戶端 ID。建立 Deployment 後 憑證,即可在「Credentials」(憑證) 頁面查看您的用戶端 ID。按一下 用戶端 ID,以便瞭解詳情,例如用戶端密鑰、重新導向 URI、JavaScript 來源地址和電子郵件地址

若需更多資訊,請參閲 授權要求

3. 呼叫 Bid Manager API

下列分頁提供使用不同語言寫程式的快速入門導覽課程。類似商家 您也可以前往 Bid Manager API 範例

Java

  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. 載入用戶端密鑰檔案並產生授權憑證。

    首次執行這個步驟時,系統會要求您接受授權 。接受之前,請確認您已使用 有權存取多媒體廣告聯播網和設定的 Google 帳戶Video 360。您的應用程式將獲得授權 以目前登入的帳戶存取資料。

    // 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 範例

Python

  1. 匯入必要的程式庫。

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
    
  2. 載入用戶端密鑰檔案並產生授權憑證。

    首次執行這個步驟時,系統會要求您接受授權 。接受之前,請確認您已使用 有權存取多媒體廣告聯播網和設定的 Google 帳戶Video 360。您的應用程式將獲得授權 以目前登入的帳戶存取資料。

    # 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 範例

PHP

此範例假設您使用內建網路伺服器執行 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. 設定用戶端,視需要重新導向至驗證網址,並擷取存取權杖。

    首次執行這個步驟時,系統會要求您接受授權 。接受之前,請確認您已使用 有權存取多媒體廣告聯播網和設定的 Google 帳戶Video 360。您的應用程式將獲得授權 以目前登入的帳戶存取資料。

    // 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. 建立多媒體廣告的客戶,Video 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 範例

4. 後續步驟

建立並執行用戶端程式庫後,請參考參考資料 說明文件並開始建構實作內容。

想查看其他指南嗎? 運用定期報表遵循檢舉最佳做法