ติดตั้งและกำหนดค่าไคลเอ็นต์

เราขอแนะนำให้คุณใช้ไลบรารีของไคลเอ็นต์ Google API กับ Display & Video 360 API การใช้ไลบรารีของไคลเอ็นต์ช่วยให้คุณไม่ต้องส่งคำขอ HTTP และแยกวิเคราะห์การตอบกลับด้วยตนเอง ไลบรารีของไคลเอ็นต์มีการผสานรวมภาษาที่ดีกว่า ความปลอดภัยที่ดียิ่งขึ้น และการรองรับการเรียกที่ต้องมีการให้สิทธิ์ผู้ใช้

Display & Video 360 API สร้างขึ้นในรูปแบบ HTTP และ JSON คุณสามารถใช้ไคลเอ็นต์ HTTP มาตรฐานเพื่อส่งคำขอและแยกวิเคราะห์การตอบกลับได้หากต้องการ

ติดตั้งไลบรารีของไคลเอ็นต์

เรามีไลบรารีของไคลเอ็นต์ที่รองรับ Display & Video 360 API ในภาษาโปรแกรมต่างๆ ดูรายการไลบรารีของไคลเอ็นต์ทั้งหมดได้ที่หน้าตัวอย่างและไลบรารี

คู่มือนักพัฒนาซอฟต์แวร์ Display & Video 360 API มีข้อมูลโค้ดสำหรับภาษาต่อไปนี้

นอกเหนือจากข้อมูลโค้ดในคำแนะนำแล้ว เรายังมีตัวอย่างการผสานรวมแบบเต็ม ในภาษาต่อไปนี้ ดูตัวอย่างเหล่านี้ได้ในที่เก็บ Display & Video 360 API examples GitHub

กำหนดค่าไคลเอ็นต์

เมื่อมีข้อมูลเข้าสู่ระบบ OAuth 2.0 และไลบรารีของไคลเอ็นต์ที่ติดตั้ง คุณก็พร้อมที่จะใช้ Display & Video 360 API วิธี ให้สิทธิ์และกำหนดค่าไคลเอ็นต์มีดังนี้

Java

  1. นำเข้าไลบรารีที่จำเป็น

    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.v4.DisplayVideo;
    import com.google.api.services.displayvideo.v4.DisplayVideo.Advertisers;
    import com.google.api.services.displayvideo.v4.model.Advertiser;
    import com.google.api.services.displayvideo.v4.model.ListAdvertisersResponse;
    import java.io.Reader;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
  2. โหลดไฟล์รหัสลับไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์

    ครั้งแรกที่คุณทำตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ ในเบราว์เซอร์ ก่อนยอมรับ โปรดตรวจสอบว่าคุณลงชื่อเข้าใช้ด้วย บัญชี 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");
    
  3. สร้างไคลเอ็นต์ API ที่ได้รับสิทธิ์

    // Create authorized API client.
    DisplayVideo service =
        new DisplayVideo.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("displayvideo-java-installed-app-sample")
            .build();
    

Python

  1. นำเข้าไลบรารีที่จำเป็น

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
    
  2. โหลดไฟล์รหัสลับไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์

    ครั้งแรกที่คุณทำตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ ในเบราว์เซอร์ ก่อนยอมรับ โปรดตรวจสอบว่าคุณลงชื่อเข้าใช้ด้วย บัญชี 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()
    
  3. สร้างไคลเอ็นต์ API ที่ได้รับสิทธิ์

    # Build the discovery document URL.
    discovery_url = f'https://displayvideo.googleapis.com/$discovery/rest?version=v4'
    
    # Build the API service.
    service = discovery.build(
        'displayvideo',
        'v4',
        discoveryServiceUrl=discovery_url,
        credentials=credentials)
    

PHP

ตัวอย่างนี้ถือว่าคุณใช้ PHP กับเว็บเซิร์ฟเวอร์ในตัวและได้กำหนดค่าข้อมูลเข้าสู่ระบบเพื่อเปลี่ยนเส้นทางไปยังหน้าเว็บที่เกี่ยวข้องแล้ว ตัวอย่างเช่น โค้ดนี้ในไฟล์ index.php สามารถเรียกใช้ได้โดยใช้คำสั่งต่อไปนี้ และข้อมูลเข้าสู่ระบบที่กำหนดค่าให้เปลี่ยนเส้นทางไปยัง http://localhost:8000 หลังจาก การตรวจสอบสิทธิ์

php -S localhost:8000 -t ./

  1. ดาวน์โหลดและติดตั้ง Google API PHP Client

    วิธีที่แนะนำคือการใช้ Composer ดังนี้

    composer require google/apiclient:^2.18.2 google/apiclient-services:=0.396.0

    เมื่อติดตั้งแล้ว อย่าลืมใส่ตัวโหลดอัตโนมัติ

    require_once '/path/to/your-project/vendor/autoload.php';
    
  2. สร้างออบเจ็กต์ Google_Client

    $client = new Google_Client();
    
  3. ตั้งค่าไคลเอ็นต์ เปลี่ยนเส้นทางไปยัง URL การตรวจสอบสิทธิ์หากจำเป็น และดึงโทเค็นเพื่อการเข้าถึง

    ครั้งแรกที่คุณทำตามขั้นตอนนี้ ระบบจะขอให้คุณยอมรับข้อความแจ้งการให้สิทธิ์ ในเบราว์เซอร์ ก่อนยอมรับ โปรดตรวจสอบว่าคุณลงชื่อเข้าใช้ด้วย บัญชี Google ที่มีสิทธิ์เข้าถึง 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);
    
  4. สร้างไคลเอ็นต์สำหรับบริการ Display & Video 360 API

    $service = new Google_Service_DisplayVideo($client);