本文档介绍如何着手编写使用 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."); }
如需详细了解如何在 Java 中使用 Bid Manager API,请参阅 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.')
如需详细了解如何在 Python 中使用 Bid Manager API,请参阅 Bid Manager API 示例中的 README 文件。
PHP
此示例假定您在内置 Web 服务器中运行 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>'; }
如需详细了解如何在 PHP 中使用 Bid Manager API,请参阅 Bid Manager API 示例中的 README 文件。
4. 后续步骤
现在您已经启动并运行了客户端库,接下来请浏览参考文档并开始构建您的实现。