Menginstal dan mengonfigurasi klien

Sebaiknya gunakan library klien Google API dengan Bid Manager API, jadi tidak perlu memproses permintaan dan respons HTTP secara manual. Google API {i>library<i} klien dapat menyediakan integrasi bahasa yang lebih baik, peningkatan keamanan, dan dukungan untuk melakukan panggilan yang membutuhkan otorisasi pengguna.

API Bid Manager dibuat berdasarkan HTTP dan JSON. Jika Anda memilih untuk memproses permintaan dan respons secara manual, Anda dapat menggunakan klien HTTP standar.

Menginstal library klien

Kami menawarkan library klien yang mendukung API Bid Manager dalam berbagai yang berpusat pada data (data-centric). Untuk daftar lengkap library klien, lihat Contoh dan tab library.

Panduan developer Bid Manager API memberikan cuplikan kode untuk tiga bahasa:

Untuk contoh integrasi lengkap dalam bahasa ini, lihat contoh Bid Manager API kami Repositori GitHub.

Mengonfigurasi klien Anda

Dengan kredensial OAuth 2.0 dan klien yang terinstal library, Anda siap menggunakan API Bid Manager. Berikut cara mengizinkan dan mengonfigurasi klien Anda:

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 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 otorisasi di browser Anda. 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 mana pun yang saat ini masuk.

    // 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();
    

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 otorisasi di browser Anda. 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 mana pun yang saat ini masuk.

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

PHP

Contoh ini mengasumsikan bahwa Anda menjalankan PHP dengan server web bawaan dan telah mengonfigurasi kredensial Anda untuk mengalihkan ke halaman web yang relevan. Sebagai sebagai contoh, kode ini dalam file index.php dapat dijalankan menggunakan dan kredensial 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 otorisasi di browser Anda. 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 mana pun yang saat ini masuk.

    // 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. Membuat klien untuk kampanye Display & Layanan Video 360 API.

    $service = new Google_Service_DoubleClickBidManager($client);