Tài liệu này giải thích cách bắt đầu viết các ứng dụng sử dụng API Google Bid Manager. API này cho phép bạn quản lý Truy vấn và truy xuất siêu dữ liệu Báo cáo.
Bid Manager API phiên bản 2 là phiên bản mới nhất hiện có và được đề xuất.
1. Trước khi bắt đầu
Nếu bạn chưa quen với các khái niệm về Google Display & Video 360, hãy đọc Trung tâm trợ giúp Display & Video 360 và thử nghiệm với giao diện người dùng.
2. Chuẩn bị cho quy trình xác thực
Để bắt đầu sử dụng Bid Manager API, trước tiên bạn cần sử dụng công cụ thiết lập. Công cụ này sẽ hướng dẫn bạn quy trình tạo dự án trong Google API Console và bật API.
Nếu chưa thực hiện, hãy tạo thông tin xác thực OAuth 2.0 bằng cách nhấp vào Tạo thông tin xác thực > Mã ứng dụng khách OAuth. Sau khi tạo thông tin xác thực, bạn có thể xem mã ứng dụng khách trên trang Thông tin xác thực. Nhấp vào mã ứng dụng để xem thông tin chi tiết, chẳng hạn như khoá bí mật của ứng dụng, URI chuyển hướng, địa chỉ nguồn JavaScript và địa chỉ email.Để biết thêm thông tin, hãy xem phần Uỷ quyền yêu cầu.
3. Gọi Bid Manager API
Các thẻ bên dưới cung cấp hướng dẫn nhanh để viết mã bằng nhiều ngôn ngữ. Bạn cũng có thể tìm thấy mã mẫu tương tự trong kho lưu trữ Ví dụ về Bid Manager API.
Java
Nhập các thư viện cần thiết.
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;
Tải tệp khoá bí mật của ứng dụng và tạo thông tin xác thực uỷ quyền.
Trong lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận một lời nhắc uỷ quyền trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng một Tài khoản Google có quyền truy cập vào Display & Video 360. Ứng dụng của bạn sẽ được uỷ quyền truy cập vào dữ liệu thay cho tài khoản hiện đang đăng nhập.
// 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");
Tạo một ứng dụng API được uỷ quyền.
// Create authorized API client. DoubleClickBidManager service = new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("bidmanager-java-installed-app-sample") .build();
Thực hiện một thao tác.
// 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."); }
Để biết thêm thông tin chi tiết về cách sử dụng Bid Manager API với Java, hãy tham khảo tệp README trong các ví dụ về Bid Manager API.
Python
Nhập các thư viện cần thiết.
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
Tải tệp khoá bí mật của ứng dụng và tạo thông tin xác thực uỷ quyền.
Trong lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận một lời nhắc uỷ quyền trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng một Tài khoản Google có quyền truy cập vào Display & Video 360. Ứng dụng của bạn sẽ được uỷ quyền truy cập vào dữ liệu thay cho tài khoản hiện đang đăng nhập.
# 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()
Tạo một ứng dụng API được uỷ quyền.
# 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)
Thực hiện một thao tác.
# 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.')
Để biết thêm thông tin chi tiết về cách sử dụng Bid Manager API với Python, hãy tham khảo tệp README trong các ví dụ về Bid Manager API.
PHP
Mẫu này giả định rằng bạn đang chạy PHP bằng một máy chủ web tích hợp và đã định cấu hình thông tin đăng nhập để chuyển hướng đến trang web có liên quan. Ví dụ: mã này trong tệp index.php có thể chạy bằng lệnh và thông tin đăng nhập sau đây được định cấu hình để chuyển hướng đến http://localhost:8000 sau khi xác thực:
php -S localhost:8000 -t ./Tải xuống và cài đặt Google API PHP Client.
Phương thức ưu tiên là thông qua Composer:
composer require google/apiclient:^2.12.1Sau khi cài đặt, hãy nhớ thêm trình tải tự động
require_once '/path/to/your-project/vendor/autoload.php';Tạo một đối tượng Google_Client.
$client = new Google_Client();Thiết lập ứng dụng, chuyển hướng đến URL xác thực (nếu cần) và truy xuất mã truy cập.
Trong lần đầu tiên thực hiện bước này, bạn sẽ được yêu cầu chấp nhận một lời nhắc uỷ quyền trong trình duyệt. Trước khi chấp nhận, hãy đảm bảo bạn đã đăng nhập bằng một Tài khoản Google có quyền truy cập vào Display & Video 360. Ứng dụng của bạn sẽ được uỷ quyền truy cập vào dữ liệu thay cho tài khoản hiện đang đăng nhập.
// 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);
Tạo một ứng dụng cho dịch vụ Display & Video 360 API.
$service = new Google_Service_DoubleClickBidManager($client);Thực hiện một thao tác.
// 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>'; }
Để biết thêm thông tin chi tiết về cách sử dụng API Bid Manager với PHP, hãy tham khảo tệp README trong các ví dụ về API Bid Manager.
4. Các bước tiếp theo
Giờ đây, bạn đã có một thư viện ứng dụng đang hoạt động, hãy khám phá tài liệu tham khảo và bắt đầu xây dựng quá trình triển khai.
Bạn cũng có thể xem các hướng dẫn khác về cách sử dụng báo cáo định kỳ và các phương pháp hay nhất về báo cáo.