建立 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 帳戶。您的應用程式將獲准代表目前登入的任何帳戶存取資料。如要進一步瞭解 Display & 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");
建立授權的 API 用戶端。
// Create authorized API client. DisplayVideo service = new DisplayVideo.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("displayvideo-java-installed-app-sample") .build();
執行作業。
// Perform an operation. // Retrieve and print the first ten advertisers under a partner. ListAdvertisersResponse response = service .advertisers() .list() .setPartnerId(partner-id) .setPageSize(10) .execute(); if (response.getAdvertisers().size() > 0) { for (int i = 0; i < response.getAdvertisers().size(); i++) { System.out.printf( "ID: %s Display Name: %s%n", response.getAdvertisers().get(i).getAdvertiserId(), response.getAdvertisers().get(i).getDisplayName()); } } else { System.out.print("No advertisers found."); }
Python
匯入必要的程式庫。
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
載入用戶端密鑰檔案並產生授權憑證。
首次執行這個步驟時,系統會要求您在瀏覽器中接受授權提示。接受前,請確認您已登入具有 Display & Video 360 存取權的 Google 帳戶。您的應用程式將獲准代表目前登入的任何帳戶存取資料。如要進一步瞭解 Display & 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()
建立授權的 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)
執行作業。
# Build advertisers.list request. request = service.advertisers().list( partnerId=partner-id, pageSize='10') # Execute request. response = request.execute() # Print response. if len(response['advertisers']) > 0: for advertiser in response['advertisers']: print(f'ID: {advertiser["advertiserId"]} Display Name: {advertiser["displayName"]}') else: print('No advertisers found.')
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 帳戶。您的應用程式將獲准代表目前登入的任何帳戶存取資料。如要進一步瞭解 Display & Video 360 使用者權限,請參閱「授權要求」指南。
// 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);
執行作業。
// Configure params for the advertisers.list request. $optParams = array('pageSize' => 10, 'partnerId' => partner-id); // Execute the request. $result = $service->advertisers->listAdvertisers($optParams); // Print the retrieved advertisers. if (!empty($result->getAdvertisers())) { print('<pre>'); foreach ($result->getAdvertisers() as $advertiser) { printf('<p>ID: %s, Display Name: %s</p>', $advertiser->advertiserId, $advertiser->displayName); } print('</pre>'); } else { print '<p>No advertisers found.</p>'; }