Configure your client

With OAuth 2.0 credentials created and a client library installed, you're ready to start using the Display & Video 360 API. Learn how to authorize, configure your client, and make your first request by following the quickstart below.

Java

  1. Load the client secrets file and generate authorization credentials.

    The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Display & Video 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.

    // Load the client secrets JSON file.
    GoogleClientSecrets clientSecrets =
      GoogleClientSecrets.load(
        jsonFactory,
        Files.newBufferedReader(
          Paths.get(path-to-client-secrets-file),UTF_8));
    
    // Set up the authorization code flow.
    //
    // Note: providing a DataStoreFactory allows auth credentials to be
    // cached, so they survive multiple runs of the program. This avoids
    // prompting the user for authorization every time the access token
    // expires, by remembering the refresh token.
    GoogleAuthorizationCodeFlow flow =
      new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, jsonFactory, clientSecrets, oauth-scopes)
        .setDataStoreFactory(dataStoreFactory)
        .build();
    
    // Authorize and persist credentials to the data store.
    //
    // Note: the "user" value below is used to identify a specific set of
    // credentials in the data store. You may provide different values
    // here to persist credentials for multiple users to the same data
    // store.
    Credential credential =
      new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
        .authorize("user");
    
  2. Create an authorized API client.

    // Create a DisplayVideo client instance.
    //
    // Note: application name below should be replaced with a value that
    // identifies your application. Suggested format is
    // "MyCompany-ProductName/Version.MinorVersion".
    DisplayVideo service =
      new DisplayVideo.Builder(
        credential.getTransport(),
        credential.getJsonFactory(),
        credential)
          .setApplicationName("displayvideo-java-installed-app-sample")
          .build();
    
  3. Perform an operation.

    // Retrieve and print the first ten advertisers under a partner.
    ListAdvertisersResponse advertisersResponse =
      service
        .advertisers()
        .list()
        .setPartnerId(partner-id)
        .setPageSize(10)
        .execute();
    for (int i = 0; i < advertisersResponse.getAdvertisers().size(); i++) {
      System.out.printf(
        "%d. %s%n",
        i + 1,
        advertisersResponse.getAdvertisers().get(i).getDisplayName());
    }
    

Python

  1. Load the client secrets file and generate authorization credentials.

    The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Display & Video 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.

    # Set up a Flow object to be used if we need to authenticate.
    flow = client.flow_from_clientsecrets(
        path-to-client-secrets-file,
        scope=oauth-scopes)
    
    # Check whether credentials exist in the credential store. Using a
    # credential store allows auth credentials to be cached, so they
    # survive multiple runs of the application. This avoids prompting the
    # user for authorization every time the access token expires, by
    # remembering the refresh token.
    storage = Storage(credential-store-file)
    credentials = storage.get()
    
    # If no credentials were found, go through the authorization process
    # and persist credentials to the credential store.
    if credentials is None or credentials.invalid:
      credentials = tools.run_flow(flow, storage)
    
    # Use the credentials to authorize an httplib2.Http instance.
    http = credentials.authorize(httplib2.Http())
    
  2. Create an authorized API client.

    # Construct a service object via the discovery service url.
    service = discovery.build('displayvideo', 'v1',
                            discoveryServiceUrl=discovery_url,
                            http=http)
    
  3. Perform an operation.

    # Construct the request to get the first 10 advertisers under a
    # partner.
    request = service.advertisers().list(
        partnerId='partner-id', pageSize='10')
    
    # Execute request and print response.
    response = request.execute()
    
    for advertiser in response['advertisers']:
      print('Found advertiser with ID %s and display name "%s".' %
          (advertiser['advertiserId'], advertiser['displayName']))
    

PHP

  1. Load the client secrets file and, if necessary generate authorization URL

    The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Display & Video 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.

    // Set up authentication.
    $client = new Google_Client();
    $client->setApplicationName('displayvideo-php-installed-app-sample');
    $client->addScope(oauth-scopes);
    $client->setAccessType('offline');
    
    $client->setAuthConfigFile(path-to-client-secrets-file);
    
    // If we have an access token, we can make requests, else we generate an
    // authentication URL.
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
    } elseif (
        file_exists(credential-store-file)
        && filesize(credential-store-file) > 0
    ) {
        // Note that "setAccessToken" actually sets both the access and refresh
        // token, assuming both were saved.
        $client->setAccessToken(file_get_contents(credential-store-file));
        $_SESSION['access_token'] = $client->getAccessToken();
    } else {
        // If we're doing disk storage, generate a URL that forces user approval.
        // This is the only way to guarantee we get back a refresh token.
        if (STORE_ON_DISK) {
            $client->setApprovalPrompt('force');
        }
        $authUrl = $client->createAuthUrl();
    }
    
    1. If needed, visit the authorization URL.
    2. Afterwards, authenticate with code, store retrieved tokens, and redirect to app page.

      if (isset($_GET['code'])) {
          $client->authenticate($_GET['code']);
          // Note that "getAccessToken" actually retrieves both the access
          // and refresh tokens, assuming both are available.
          $_SESSION['access_token'] = $client->getAccessToken();
          if (STORE_ON_DISK) {
              file_put_contents(
                  TOKEN_FILENAME,
                  json_encode($_SESSION['access_token'])
              );
          }
          $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
          header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
          exit;
      }
      
  2. Create an authorized API client.

    // Construct a service object.
    $service = new Google_Service_DisplayVideo($client);
    
  3. Perform an operation.

    // Construct the request parameters to get the first 10 advertisers
    // under a partner.
    $optParams = array(
        'partnerId' => partner-id
        'pageSize' => '10'
    );
    
    // Call the API.
    $response = $this
        ->service
        ->advertisers
        ->listAdvertisers($optParams);
    
    // Print response.
    foreach ($response->getAdvertisers() as $advertiser) {
        printf(
            'Found advertiser with ID %s and display name %s\n',
            $advertiser['advertiserId'],
            $advertiser['displayName']
        );
    }