本文档将介绍如何开始使用 Google Bid Manager API 编写应用。借助该 API,您可以管理查询和检索报告元数据。
Bid Manager API v2 是最新的可用版本和推荐版本。
1. 前期准备
如果您不熟悉 Google Display & Video 360 概念,请阅读 Display & Video 360 帮助中心,并试用界面。
2. 为身份验证做好准备
要开始使用 Bid Manager API,您需要先使用设置工具,该工具会引导您在 Google API 控制台中创建项目、启用 API 以及创建凭据。
如果您尚未创建 OAuth 2.0 凭据,请点击创建凭据 > OAuth 客户端 ID(如果尚未创建)。创建凭据后,您可以在凭据页面上查看您的客户端 ID。点击客户端 ID 了解详情,例如客户端密钥、重定向 URI、JavaScript 源地址和电子邮件地址。如需了解详情,请参阅授权请求。
3. 调用 Bid Manager API
以下标签页提供了各种语言的编码快速入门。类似的示例代码也可以在 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 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;
加载客户端密钥文件并生成授权凭据。
首次执行此步骤时,系统会要求您在浏览器中接受授权提示。接受之前,请务必使用可访问 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();
执行操作。
// 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."); }
如需详细了解如何将 Bid Manager API 与 Java 搭配使用,请参阅 Bid Manager API 示例中的 README 文件。
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://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2' # Build the API service. service = discovery.build( 'doubleclickbidmanager', 'v2', discoveryServiceUrl=discovery_url, credentials=credentials)
执行操作。
# 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.')
如需详细了解如何将 Bid Manager API 与 Python 搭配使用,请参阅 Bid Manager API 示例中的 README 文件。
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);
执行操作。
// 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>'; }
如需详细了解如何将 Bid Manager API 与 PHP 搭配使用,请参阅 Bid Manager API 示例中的 README 文件。
4. 后续步骤
现在您已经有了客户端库并可以运行了,请浏览参考文档并开始构建实现。