我们建议您将 Google API 客户端库与 Bid Manager API 搭配使用,这样您就不必手动处理 HTTP 请求和响应。Google API 客户端库可提供更好的语言集成和更高的安全性,还支持进行需要用户授权的调用。
Bid Manager API 基于 HTTP 和 JSON 构建。如果您希望手动处理请求和响应,可以使用任何标准 HTTP 客户端。
安装客户端库
我们提供了多种编程语言的客户端库,这些库支持 Bid Manager API。如需查看客户端库的完整列表,请参阅“示例和 库”标签页。
Bid Manager API 开发者指南提供了以下三种语言的代码段:
如需查看这些语言的完整集成示例,请参阅我们的 Bid Manager API 示例 GitHub 代码库。
配置客户端
有了 OAuth 2.0 凭据和已安装的客户端库,您就可以使用 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 java.io.Reader; import java.nio.file.Files; import java.nio.file.Paths;
加载 Secret 文件并生成授权凭据。
首次执行此步骤时,系统会在浏览器中提示您接受授权。接受之前,请务必使用可访问 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();
Python
导入必要的库。
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
加载 Secret 文件并生成授权凭据。
首次执行此步骤时,系统会在浏览器中提示您接受授权。接受之前,请务必使用可访问 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)
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);