建議您搭配使用 Google API 用戶端程式庫與 Bid Manager 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
匯入必要的程式庫。
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;
載入 Secrets 檔案,並產生授權憑證。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請務必登入有權存取 Display & Video 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();
Python
匯入必要的程式庫。
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
載入 Secrets 檔案,並產生授權憑證。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請務必登入有權存取 Display & Video 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)
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();設定用戶端、視需要重新導向至驗證網址,並擷取存取權杖。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請務必登入有權存取 Display & Video 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);