เราขอแนะนำให้ใช้ไลบรารีของไคลเอ็นต์ Google API กับ Bid Manager API เพื่อให้คุณไม่ต้องประมวลผลคำขอและการตอบกลับ HTTP ด้วยตนเอง ไลบรารีของไคลเอ็นต์ Google API มีการผสานรวมภาษาที่ดีกว่า ความปลอดภัยที่ดียิ่งขึ้น และการรองรับการเรียกที่ต้องมีการให้สิทธิ์ผู้ใช้
Bid Manager API สร้างขึ้นในรูปแบบ HTTP และ JSON หากต้องการประมวลผลคำขอและการตอบกลับด้วยตนเอง คุณสามารถใช้ไคลเอ็นต์ HTTP มาตรฐานต่างๆ ได้
ติดตั้งไลบรารีของไคลเอ็นต์
เรามีไลบรารีของไคลเอ็นต์ที่รองรับ Bid Manager API ในภาษาโปรแกรมต่างๆ ดูรายการไลบรารีของไคลเอ็นต์ทั้งหมดได้ในแท็บตัวอย่างและ ไลบรารี
คู่มือสำหรับนักพัฒนาแอป Bid Manager API มีข้อมูลโค้ดสำหรับ 3 ภาษาต่อไปนี้
ดูตัวอย่างการผสานรวมทั้งหมดในภาษาเหล่านี้ได้ในที่เก็บของ Bid Manager API examples 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;
โหลดไฟล์ข้อมูลลับของไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์
เมื่อทำตามขั้นตอนนี้เป็นครั้งแรก ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ โปรดตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง 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. 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
โหลดไฟล์ข้อมูลลับของไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์
เมื่อทำตามขั้นตอนนี้เป็นครั้งแรก ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ โปรดตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง 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://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2' # Build the API service. service = discovery.build( 'doubleclickbidmanager', 'v2', discoveryServiceUrl=discovery_url, credentials=credentials)
PHP
ตัวอย่างนี้ถือว่าคุณกำลังเรียกใช้ PHP กับเว็บเซิร์ฟเวอร์ในตัวและได้กำหนดค่าข้อมูลเข้าสู่ระบบเพื่อเปลี่ยนเส้นทางไปยังหน้าเว็บที่เกี่ยวข้อง ตัวอย่างเช่น คุณสามารถเรียกใช้โค้ดนี้ในไฟล์ index.php ได้โดยใช้คำสั่งต่อไปนี้และข้อมูลเข้าสู่ระบบที่กำหนดค่าให้เปลี่ยนเส้นทางไปยัง http://localhost:8000 หลังจากการตรวจสอบสิทธิ์
php -S localhost:8000 -t ./ดาวน์โหลดและติดตั้ง Google API PHP Client
วิธีที่แนะนำคือผ่าน Composer:
composer require google/apiclient:^2.12.1เมื่อติดตั้งแล้ว ให้ใส่ Autoloader
require_once '/path/to/your-project/vendor/autoload.php';สร้างออบเจ็กต์ Google_Client
$client = new Google_Client();ตั้งค่าไคลเอ็นต์ เปลี่ยนเส้นทางไปยัง URL การตรวจสอบสิทธิ์หากจำเป็น และดึงข้อมูลโทเค็นเพื่อการเข้าถึง
เมื่อทำตามขั้นตอนนี้เป็นครั้งแรก ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ในเบราว์เซอร์ โปรดตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วยบัญชี Google ที่มีสิทธิ์เข้าถึง Display & Video 360 ก่อนที่จะยอมรับ ระบบจะให้สิทธิ์แอปของคุณในการเข้าถึงข้อมูลในนามของบัญชีที่ลงชื่อเข้าใช้อยู่ในปัจจุบัน
// 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);