Mulai Menggunakan API

Dokumen ini menjelaskan cara mulai menulis aplikasi yang menggunakan Google Bid Manager API. API memungkinkan Anda mengelola Kueri dan pengambilan Metadata laporan.

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

1. Sebelum memulai

Jika Anda tidak memahami konsep Google Display & Video 360, baca Pusat Bantuan Display & Video 360 dan lakukan eksperimen dengan UI.

2. Bersiap untuk autentikasi

Untuk mulai menggunakan Bid Manager API, Anda harus terlebih dahulu penggunaan alat penyiapan, yang akan memandu Anda membuat project di Konsol API Google, mengaktifkan API, dan membuat kredensial.

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

Untuk informasi selengkapnya, lihat Mengizinkan Permintaan.

3. Memanggil Bid Manager API

Tab di bawah ini menyediakan panduan memulai untuk coding dalam berbagai bahasa. Kode contoh yang serupa juga dapat ditemukan di repo Contoh Bid Manager API.

JavaPythonPHP
  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 menerima, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diberi otorisasi untuk mengakses data atas nama akun yang saat ini digunakan untuk 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. Melakukan 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 informasi lebih detail tentang penggunaan Bid Manager API dengan Java, lihat README file dalam Contoh API Bid Manager.

  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 menerima, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diberi otorisasi untuk mengakses data atas nama akun yang saat ini digunakan untuk 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. Melakukan 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 informasi yang lebih mendetail tentang penggunaan Bid Manager API dengan Python, lihat file README di contoh Bid Manager API.

Contoh ini mengasumsikan bahwa Anda menjalankan PHP dengan server web bawaan dan telah mengonfigurasi kredensial 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 direkomendasikan 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. Membuat 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 menerima, pastikan Anda login dengan Akun Google yang memiliki akses ke Display & Video 360. Aplikasi Anda akan diberi otorisasi untuk mengakses data atas nama akun yang saat ini digunakan untuk 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. Menjalankan 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 informasi yang lebih mendetail tentang cara menggunakan Bid Manager API dengan PHP, lihat file README di contoh Bid Manager API.

4. Langkah berikutnya

Setelah Anda memiliki library klien yang aktif dan berjalan, pelajari referensi dokumentasi dan mulai membangun implementasi Anda.

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