เริ่มต้นใช้งาน API

เอกสารนี้อธิบายวิธีเริ่มต้นเขียนแอปที่ใช้ Google Bid Manager API API ช่วยให้คุณจัดการได้ คำค้นหา และเรียกข้อมูล ข้อมูลเมตารายงาน

Bid Manager API v2 เป็นเวอร์ชันล่าสุดและที่แนะนำ เวอร์ชัน

1. ก่อนจะเริ่ม

หากคุณไม่คุ้นเคยกับการแสดงผลและ แนวคิดของ Video 360 โปรดอ่าน จอแสดงผลและ ศูนย์ช่วยเหลือของ Video 360 และ ทดสอบกับ UI

2. เตรียมพร้อมสำหรับการตรวจสอบสิทธิ์

หากต้องการเริ่มต้นใช้งาน Bid Manager API ก่อนอื่นคุณต้อง ใช้ เครื่องมือตั้งค่า ซึ่งจะแนะนำขั้นตอนการสร้างโปรเจ็กต์ใน คอนโซล Google API, การเปิดใช้ API และการสร้างข้อมูลเข้าสู่ระบบ

หากยังไม่ได้ดำเนินการ ให้สร้างข้อมูลเข้าสู่ระบบ OAuth 2.0 โดยคลิก สร้างข้อมูลเข้าสู่ระบบ > รหัสไคลเอ็นต์ OAuth หลังจากที่คุณสร้าง ข้อมูลเข้าสู่ระบบ คุณจะเห็นรหัสไคลเอ็นต์ในหน้าข้อมูลเข้าสู่ระบบ คลิก รหัสไคลเอนต์สำหรับรายละเอียด เช่น รหัสลับไคลเอ็นต์, URI การเปลี่ยนเส้นทาง, JavaScript อีเมลต้นทางและอีเมล

สำหรับข้อมูลเพิ่มเติม โปรดดู ให้สิทธิ์คำขอ

3. เรียกใช้ Bid Manager API

แท็บด้านล่างจะแสดงการเริ่มต้นอย่างรวดเร็วสำหรับการเขียนโค้ดในภาษาต่างๆ คล้ายกัน คุณสามารถดูโค้ดตัวอย่างได้ที่ ตัวอย่าง API ของ Bid Manager ที่เก็บ

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.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;
    
  2. โหลดไฟล์รหัสลับไคลเอ็นต์และสร้างข้อมูลเข้าสู่ระบบการให้สิทธิ์

    ครั้งแรกที่ดำเนินการขั้นตอนนี้ ระบบจะขอให้คุณยอมรับการให้สิทธิ์ ในเบราว์เซอร์ ก่อนที่จะยอมรับ โปรดตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วย บัญชี Google ที่มีสิทธิ์เข้าถึงดิสเพลย์และ วิดีโอ 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.
    DoubleClickBidManager service =
        new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("bidmanager-java-installed-app-sample")
            .build();
    
  4. ดำเนินการ

    // 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.");
    }
    

สำหรับข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับการใช้ Bid Manager API กับ Java อ้างอิง README ในไฟล์ ตัวอย่าง API ของ Bid Manager

Python

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

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

    ครั้งแรกที่ดำเนินการขั้นตอนนี้ ระบบจะขอให้คุณยอมรับการให้สิทธิ์ ในเบราว์เซอร์ ก่อนที่จะยอมรับ โปรดตรวจสอบว่าคุณได้ลงชื่อเข้าใช้ด้วย บัญชี Google ที่มีสิทธิ์เข้าถึงดิสเพลย์และ วิดีโอ 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://doubleclickbidmanager.googleapis.com/$discovery/rest?version=v2'
    
    # Build the API service.
    service = discovery.build(
        'doubleclickbidmanager',
        'v2',
        discoveryServiceUrl=discovery_url,
        credentials=credentials)
    
  4. ดำเนินการ

    # 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.')
    

สำหรับข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับการใช้ Bid Manager API กับ Python อ้างอิง README ในไฟล์ ตัวอย่าง API ของ Bid Manager

PHP

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

php -S localhost:8000 -t ./

  1. ดาวน์โหลดและติดตั้งไคลเอ็นต์ Google API PHP

    วิธีที่แนะนำคือการใช้คอมโพสเซอร์ ดังนี้

    composer require google/apiclient:^2.12.1
    

    เมื่อติดตั้งแล้ว โปรดตรวจสอบว่าได้รวมตัวโหลดอัตโนมัติแล้ว

    require_once '/path/to/your-project/vendor/autoload.php';
    

  2. สร้างออบเจ็กต์ Google_Client

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

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

    $service = new Google_Service_DoubleClickBidManager($client);
    
  5. ดำเนินการ

    // 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>';
    }
    

สำหรับข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับการใช้ Bid Manager API กับ PHP อ้างอิง README ในไฟล์ ตัวอย่าง API ของ Bid Manager

4. ขั้นตอนถัดไป

ตอนนี้คุณมีไลบรารีของไคลเอ็นต์ที่กำลังทำงานอยู่แล้ว ลองสำรวจข้อมูลอ้างอิง เอกสาร และเริ่มนำไปปรับใช้ได้เลย

คุณสามารถดูคำแนะนำเพิ่มเติมได้ที่ การใช้รายงานที่ตั้งเวลาไว้ และ ทำตามแนวทางปฏิบัติแนะนำสำหรับการรายงาน