Install and configure your client

We recommend that you use the Google API client libraries with the Display & Video 360 API. By using client libraries, you don't have to manually make HTTP requests and parse responses. Client libraries can provide better language integration, improved security, and support for making calls that require user authorization.

The Display & Video 360 API is built on HTTP and JSON. If you prefer, you can use any standard HTTP client to make requests and parse responses.

Install a client library

We offer client libraries that support the Display & Video 360 API in a variety of programming languages. For a full list of client libraries, see our Samples and Libraries page.

The Display & Video 360 API developer guides provide code snippets for the following languages:

In addition to code snippets in our guides, we offer full integration examples in these languages. See our Display & Video 360 API examples GitHub repository for these samples.

Configure your client

With OAuth 2.0 credentials and an installed client library, you're ready to use the Display & Video 360 API. Here's how to authorize and configure your client:

Java

  1. Import the necessary libraries.

    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. Load the client secrets file and generate authorization credentials.

    The first time you perform this step, you are asked to accept an authorization prompt in your browser. Before accepting, make sure you're signed in with a Google Account that has access to Display & Video 360. Your app will be authorized to access data on behalf of whichever account is currently signed in. See our User Permissions concept guide guide for more details.

    // 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. Create an authorized API client.

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

Python

  1. Import the necessary libraries.

    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import discovery
    
  2. Load the client secrets file and generate authorization credentials.

    The first time you perform this step, you are asked to accept an authorization prompt in your browser. Before accepting, make sure you're signed in with a Google Account that has access to Display & Video 360. Your app will be authorized to access data on behalf of whichever account is currently signed in. See our User Permissions concept guide guide for more details.

    # 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. Create an authorized API client.

    # 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

This sample assumes that you are running the PHP with a built-in web server and have configured your credentials to redirect to the relevant web page. For example, this code, in an index.php file, can be run using the following command and credentials configured to redirect to http://localhost:8000 after authentication:

php -S localhost:8000 -t ./

  1. Download and install the Google API PHP Client.

    The preferred method is using Composer:

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

    Once installed, be sure to include the autoloader:

    require_once '/path/to/your-project/vendor/autoload.php';
    
  2. Create a Google_Client object.

    $client = new Google_Client();
    
  3. Set up the client, redirect to authentication URL if needed, and retrieve an access token.

    The first time you perform this step, you are asked to accept an authorization prompt in your browser. Before accepting, make sure you're signed in with a Google Account that has access to Display & Video 360. Your app will be authorized to access data on behalf of whichever account is currently signed in. See our User Permissions concept guide guide for more details.

    // 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. Construct a client for the Display & Video 360 API service.

    $service = new Google_Service_DisplayVideo($client);