创建了 OAuth 2.0 凭据和客户端库 安装,就可以开始使用展示广告和Video 360 API。 通过以下方式了解如何授权、配置客户端和发出第一个请求: 按照以下快速入门操作。
导入必要的库。
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.v3.DisplayVideo;
import com.google.api.services.displayvideo.v3.DisplayVideo.Advertisers;
import com.google.api.services.displayvideo.v3.model.Advertiser;
import com.google.api.services.displayvideo.v3.model.ListAdvertisersResponse;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;加载客户端密钥文件并生成授权凭据。
首次执行此步骤时,系统会要求您接受授权 。接受之前,请确保您已使用 有权访问展示广告网络和Video 360。您的应用将获得授权 来代表当前登录的账号访问数据。请参阅我们的 授权 请求指南 请访问发布商学院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.");
}
导入必要的库。
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient import discovery加载客户端密钥文件并生成授权凭据。
首次执行此步骤时,系统会要求您接受授权 。接受之前,请确保您已使用 有权访问展示广告网络和Video 360。您的应用将获得授权 来代表当前登录的账号访问数据。请参阅我们的 授权 请求指南 请访问发布商学院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=v3'
# Build the API service.
service = discovery.build(
'displayvideo',
'v3',
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.')
此示例假定您运行的是带有内置 Web 服务器的 PHP,并且
已将您的凭据配置为重定向到相关网页。对于
例如,index.php
文件中的这段代码可以使用以下代码运行
命令和凭据配置为在下列时间后重定向到 http://localhost:8000
身份验证:
php -S localhost:8000 -t ./
下载并安装 Google API PHP 客户端。
首选方法是使用 Composer:
composer require google/apiclient:^2.15.1 google/apiclient-services:=0.332.0
安装后,请务必添加自动加载器:
require_once '/path/to/your-project/vendor/autoload.php';
创建一个 Google_Client 对象。
$client = new Google_Client();
设置客户端,根据需要重定向到身份验证网址,并检索访问令牌。
首次执行此步骤时,系统会要求您接受授权 。接受之前,请确保您已使用 有权访问展示广告网络和Video 360。您的应用将获得授权 来代表当前登录的账号访问数据。请参阅我们的 授权 请求指南 请访问发布商学院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);为展示广告构建客户端和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>';
}