本文档介绍了如何开始编写使用 Google 出价管理器 API 的应用。借助此 API,您可以管理查询并检索报告元数据。
Bid Manager API v2 是最新的推荐版本。
1. 前期准备
如果您不熟悉 Google 展示广告网络,Video 360 概念,请阅读 展示广告与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 Examples 代码库中找到类似的示例代码。
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 请参阅 README 文件(位于 Bid Manager API 示例。
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 请参阅 README 文件(位于 Bid Manager API 示例。
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>'; }
如需详细了解如何将 Bid Manager API 与 PHP 搭配使用,请参阅 Bid Manager API 示例中的 README 文件。
4. 后续步骤
现在,您已经启动并运行了客户端库,接下来可以浏览参考文档,并开始构建您的实现。