Mulai Menggunakan API

Dokumen ini menjelaskan cara mulai menulis aplikasi yang menggunakan Google Bid Manager API. API ini memungkinkan Anda mengelola Kueri dan mengambil metadata Laporan.

Bid Manager API v2 adalah versi terbaru yang tersedia dan direkomendasikan.

1. Sebelum memulai

Jika Anda belum memahami konsep Google Display & Video 360, baca Pusat Bantuan Display & Video 360 dan bereksperimen dengan UI.

2. Bersiap untuk autentikasi

Untuk mulai menggunakan Bid Manager API, pertama-tama Anda harus menggunakan alat penyiapan, yang memandu Anda menyelesaikan pembuatan project di Konsol Google API dan mengaktifkan API.

Jika belum, buat kredensial OAuth 2.0 dengan mengklik Buat kredensial > ID klien OAuth. Setelah membuat kredensial, Anda dapat melihat ID klien Anda di halaman Kredensial. Klik ID klien untuk mengetahui detail, seperti rahasia klien, URI pengalihan, alamat asal JavaScript, dan alamat email.

Untuk mengetahui informasi selengkapnya, lihat Mengizinkan Permintaan.

3. Memanggil Bid Manager API

Tab di bawah ini menyediakan panduan memulai cepat untuk coding dalam berbagai bahasa. Contoh kode serupa juga dapat ditemukan di repo Bid Manager API Examples.

Java

  1. Impor library yang diperlukan.

    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. Muat file rahasia klien dan buat kredensial otorisasi.

    Saat pertama kali melakukan langkah ini, Anda akan diminta untuk menyetujui perintah otorisasi di browser. Sebelum menyetujui, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diizinkan untuk mengakses data atas nama akun mana pun yang saat ini login.

    // 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. Buat klien API yang diotorisasi.

    // Create authorized API client.
    DoubleClickBidManager service =
        new DoubleClickBidManager.Builder(credential.getTransport(), credential.getJsonFactory(), credential)
            .setApplicationName("bidmanager-java-installed-app-sample")
            .build();
  4. Lakukan operasi.

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

Untuk mengetahui informasi yang lebih mendetail tentang cara menggunakan Bid Manager API dengan Java, lihat file README di contoh Bid Manager API.

Python

  1. Impor library yang diperlukan.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
  2. Muat file rahasia klien dan buat kredensial otorisasi.

    Saat pertama kali melakukan langkah ini, Anda akan diminta untuk menyetujui perintah otorisasi di browser. Sebelum menyetujui, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diizinkan untuk mengakses data atas nama akun mana pun yang saat ini login.

    # 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. Buat klien API yang diotorisasi.

    # 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. Lakukan operasi.

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

Untuk mengetahui informasi yang lebih mendetail tentang penggunaan Bid Manager API dengan Python, lihat file README di contoh Bid Manager API.

PHP

Contoh ini mengasumsikan bahwa Anda menjalankan PHP dengan server web bawaan dan telah mengonfigurasi kredensial Anda untuk mengalihkan ke halaman web yang relevan. Misalnya, kode ini, dalam file index.php, dapat dijalankan menggunakan perintah dan kredensial berikut yang dikonfigurasi untuk mengalihkan ke http://localhost:8000 setelah autentikasi:

php -S localhost:8000 -t ./

  1. Download dan instal Klien PHP Google API.

    Metode yang disukai adalah melalui Composer:

    composer require google/apiclient:^2.12.1

    Setelah diinstal, pastikan untuk menyertakan autoloader

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

  2. Buat objek Google_Client.

    $client = new Google_Client();
  3. Siapkan klien, alihkan ke URL autentikasi jika diperlukan, dan ambil token akses.

    Saat pertama kali melakukan langkah ini, Anda akan diminta untuk menyetujui perintah otorisasi di browser. Sebelum menyetujui, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diizinkan untuk mengakses data atas nama akun mana pun yang saat ini login.

    // 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. Buat klien untuk layanan Display & Video 360 API.

    $service = new Google_Service_DoubleClickBidManager($client);
  5. Lakukan operasi.

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

Untuk mengetahui informasi yang lebih mendetail tentang penggunaan Bid Manager API dengan PHP, lihat file README di contoh Bid Manager API.

4. Langkah berikutnya

Setelah library klien Anda aktif dan berjalan, pelajari dokumentasi referensi dan mulailah membangun implementasi Anda.

Panduan tambahan juga tersedia tentang cara menggunakan laporan terjadwal dan cara mengikuti praktik terbaik pelaporan.