建議您搭配使用 Google API 用戶端程式庫和 Display & Video 360 API。 使用用戶端程式庫時,您不必手動發出 HTTP 要求及剖析回應。用戶端程式庫可提供更完善的語言整合、提升安全性,並支援需要使用者授權的呼叫。
Display & Video 360 API 是以 HTTP 和 JSON 為基礎建構而成。您也可以視需要使用任何標準 HTTP 用戶端發出要求及剖析回應。
安裝用戶端程式庫
我們提供支援 Display & Video 360 API 的用戶端程式庫,適用於各種程式設計語言。如需用戶端程式庫的完整清單,請參閱「範例和程式庫」頁面。
Display & Video 360 API 開發人員指南提供下列語言的程式碼片段:
除了指南中的程式碼片段,我們也提供下列語言的完整整合範例。如需這些範例,請參閱 Display & Video 360 API 範例 GitHub 存放區。
設定用戶端
有了 OAuth 2.0 憑證和已安裝的用戶端程式庫,您就可以使用 Display & Video 360 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.displayvideo.v4.DisplayVideo; import com.google.api.services.displayvideo.v4.DisplayVideo.Advertisers; import com.google.api.services.displayvideo.v4.model.Advertiser; import com.google.api.services.displayvideo.v4.model.ListAdvertisersResponse; import java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths;載入用戶端密鑰檔案,並產生授權憑證。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請務必登入有權存取 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. DisplayVideo service = new DisplayVideo.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("displayvideo-java-installed-app-sample") .build();
Python
匯入必要的程式庫。
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery載入用戶端密鑰檔案,並產生授權憑證。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請務必登入有權存取 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://displayvideo.googleapis.com/$discovery/rest?version=v4' # Build the API service. service = discovery.build( 'displayvideo', 'v4', 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.18.2 google/apiclient-services:=0.396.0安裝完成後,請務必加入自動載入器:
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('DV360 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_DisplayVideo($client);