เมื่อสร้างข้อมูลเข้าสู่ระบบ OAuth 2.0 และติดตั้งไลบรารีของไคลเอ็นต์แล้ว คุณก็พร้อมที่จะเริ่มใช้ Display & Video 360 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.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;
โหลดไฟล์รหัสลับไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์
ครั้งแรกที่คุณดำเนินการตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ของคุณ ก่อนยอมรับ ดูให้แน่ใจว่าคุณลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง Display & Video 360 แอปของคุณจะได้รับสิทธิ์เข้าถึง ข้อมูลในนามของบัญชีที่ลงชื่อเข้าใช้อยู่ ดูรายละเอียดเพิ่มเติมเกี่ยวกับสิทธิ์ของผู้ใช้ Display & 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."); }
Python
นำเข้าไลบรารีที่จำเป็น
from google_auth_oauthlib.flow import InstalledAppFlow from googleapiclient import discovery
โหลดไฟล์รหัสลับไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์
ครั้งแรกที่คุณดำเนินการตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ของคุณ ก่อนยอมรับ ดูให้แน่ใจว่าคุณลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง Display & Video 360 แอปของคุณจะได้รับสิทธิ์เข้าถึง ข้อมูลในนามของบัญชีที่ลงชื่อเข้าใช้อยู่ ดูรายละเอียดเพิ่มเติมเกี่ยวกับสิทธิ์ของผู้ใช้ Display & 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.')
PHP
ตัวอย่างนี้สมมติว่าคุณเรียกใช้ PHP ด้วยเว็บเซิร์ฟเวอร์ในตัว และได้กำหนดค่าข้อมูลรับรองของคุณให้เปลี่ยนเส้นทางไปยังหน้าเว็บที่เกี่ยวข้อง เช่น โค้ดนี้ในไฟล์ index.php
จะทำงานโดยใช้คำสั่งและข้อมูลเข้าสู่ระบบต่อไปนี้ที่กำหนดค่าให้เปลี่ยนเส้นทางไปยัง http://localhost:8000
หลังการตรวจสอบสิทธิ์
php -S localhost:8000 -t ./
ดาวน์โหลดและติดตั้งไคลเอ็นต์ PHP ของ Google API
วิธีการที่แนะนำคือการใช้ 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();
ตั้งค่าไคลเอ็นต์ เปลี่ยนเส้นทางไปยัง URL การตรวจสอบสิทธิ์หากจำเป็น และเรียกข้อมูลโทเค็นเพื่อการเข้าถึง
ครั้งแรกที่คุณดำเนินการตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ของคุณ ก่อนยอมรับ ดูให้แน่ใจว่าคุณลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง Display & Video 360 แอปของคุณจะได้รับสิทธิ์เข้าถึง ข้อมูลในนามของบัญชีที่ลงชื่อเข้าใช้อยู่ ดูรายละเอียดเพิ่มเติมเกี่ยวกับสิทธิ์ของผู้ใช้ Display & 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);
สร้างไคลเอ็นต์สำหรับบริการ API ของ Display & Video 360
$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>'; }