安裝及設定用戶端

建議您搭配 Bid Manager API 使用 Google API 用戶端程式庫,以便 不必手動處理 HTTP 要求和回應Google API 用戶端程式庫提供更佳的語言整合、更高的安全性, 支援執行需要使用者授權的呼叫。

Bid Manager API 是以 HTTP 和 JSON 為基礎建立而成,如果希望處理要求 和回應,您可以使用任何標準 HTTP 用戶端。

安裝用戶端程式庫

我們提供支援 Bid Manager API 的用戶端程式庫, 程式設計語言如需完整的用戶端程式庫清單,請參閱 程式庫分頁

Bid Manager API 開發人員指南提供下列三個項目的程式碼片段 語言:

如需這些語言的完整整合範例,請參閱 Bid Manager API 範例 GitHub 存放區

設定用戶端

使用 OAuth 2.0 憑證已安裝的用戶端 程式庫,您已準備好使用 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 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();
    

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)
    

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);