Reminder: v201611 will be sunset on November 30, 2017.

Get Started

Developers can use the DFP API to build applications that manage inventory, create orders, pull reports, and more.

The DFP API uses SOAP; to help you get started, we offer client libraries in Java, .NET, Python, PHP, and Ruby. To make your first API request, follow the steps below.

Get access to a DFP network

If you don't already have one, sign up for a DFP account. You can also create a test network if you want to test the API in a separate environment.

Make a note of your network code. You'll find this in the URL when you are logged into your network. For example, in the URL https://www.google.com/dfp/2032576#delivery, 2032576 is your network code.

Create authentication credentials

You must authenticate all DFP API requests using OAuth2. The steps below cover the simple use case of accessing your own DFP data. For more details and other options, see Authentication.

  1. Open the Google API Console Credentials page.
  2. From the project menu, choose Create project, enter a name for the project, and optionally, edit the provided Project ID. Click Create.
  3. On the Credentials page, select Create credentials, then select Service account key.
  4. Select New service account, and select JSON as the key type.
  5. Click Create to download a file containing a private key.

Configure your DFP network

  1. Go to your DoubleClick for Publishers network.
  2. Click the Admin tab.
  3. Ensure that API access is enabled.
  4. Click the Add a service account user button.
  5. Fill in the form using the service account email you created in the previous step.
  6. Click on the Save button. A message should appear, confirming the addition of your service account.

Set up your client

Download one of the DFP client libraries. The libraries offer wrapper functions and features that make it easier and faster to develop applications.

The tabs below provide quickstarts for coding in each of the languages for which there is a client library.

Java

Here is a basic example that shows how to use the Java client library.

  1. Setup your credentials

    Run the following command in a shell:

    curl https://raw.githubusercontent.com/googleads/googleads-java-lib/master/examples/dfp_axis/src/main/resources/ads.properties -o ~/ads.properties
    Open the ~/ads.properties file and populate the following fields:
    [...]
    api.dfp.applicationName=INSERT_APPLICATION_NAME_HERE
    api.dfp.jsonKeyPairPath=INSERT_PATH_TO_JSON_KEY_FILE_HERE
    api.dfp.networkCode=INSERT_NETWORK_CODE_HERE
    [...]
  2. Specify dependencies

    Edit your pom.xml file and add the following to the dependencies tag. You can find the latest version number on Github.

    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>ads-lib</artifactId>
      <version>RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>dfp-axis</artifactId>
      <version>RELEASE</version>
    </dependency>
  3. Write some code and make a request!

    import com.google.api.ads.common.lib.auth.OfflineCredentials;
    import com.google.api.ads.common.lib.auth.OfflineCredentials.Api;
    import com.google.api.ads.dfp.axis.factory.DfpServices;
    import com.google.api.ads.dfp.axis.v201708.Network;
    import com.google.api.ads.dfp.axis.v201708.NetworkServiceInterface;
    import com.google.api.ads.dfp.lib.client.DfpSession;
    import com.google.api.client.auth.oauth2.Credential;
    
    public class App {
      public static void main(String[] args) throws Exception {
        Credential oAuth2Credential = new OfflineCredentials.Builder()
            .forApi(Api.DFP)
            .fromFile()
            .build()
            .generateCredential();
    
        // Construct a DfpSession.
        DfpSession session = new DfpSession.Builder()
            .fromFile()
            .withOAuth2Credential(oAuth2Credential)
            .build();
    
        // Construct a DFP service factory, which can only be used once per
        // thread, but should be reused as much as possible.
        DfpServices dfpServices = new DfpServices();
    
        // Retrieve the appropriate service
        NetworkServiceInterface networkService = dfpServices.get(session,
            NetworkServiceInterface.class);
    
        // Make a request
        Network network = networkService.getCurrentNetwork();
    
        System.out.printf("Current network has network code '%s' and display" +
            " name '%s'.%n", network.getNetworkCode(), network.getDisplayName());
      }
    }
    

For more detailed information about using the Java Client Library, refer to the README file in the client library distribution.

Python

Here is a basic example that shows how to use the Python client library.

The Python Client Library supports Python v2.7 or Python v3.0+ via the 2to3 converter.

  1. Install the library and setup your credentials.

    Run the following commands in a shell:

    pip install googleads
    curl https://raw.githubusercontent.com/googleads/googleads-python-lib/master/googleads.yaml -o ~/googleads.yaml
      
  2. Setup your ~/googleads.yaml file.

    Fill in the following fields:

    dfp:
      application_name: INSERT_APPLICATION_NAME_HERE
      network_code: INSERT_NETWORK_CODE_HERE
      path_to_private_key_file: INSERT_PATH_TO_FILE_HERE
    
  3. Run some code and make a request!
    # Import the library.
    from googleads import dfp
    
    # Initialize a client object, by default uses the credentials in ~/googleads.yaml.
    dfp_client = dfp.DfpClient.LoadFromStorage()
    
    # Initialize a service.
    network_service = dfp_client.GetService('NetworkService', version='v201708')
    
    # Make a request.
    current_network = network_service.getCurrentNetwork()
    
    print 'Found network %s (%s)!' % (current_network['displayName'],
                                      current_network['networkCode'])
    

PHP

Here is a basic example that shows how to use the PHP client library.

  1. Install the library and setup your credentials.

    Run the following commands in a shell to install the client library and download the adsapi_php.ini file to your home directory:

    composer require googleads/googleads-php-lib
    curl https://raw.githubusercontent.com/googleads/googleads-php-lib/master/examples/Dfp/adsapi_php.ini -o ~/adsapi_php.ini
  2. Setup your ~/adsapi_php.ini file.

    Fill in the following fields:

    [DFP]
    networkCode = "INSERT_NETWORK_CODE_HERE"
    applicationName = "INSERT_APPLICATION_NAME_HERE"
    
    [OAUTH2]
    jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
    scopes = "https://www.googleapis.com/auth/dfp"
    
  3. Run some code and make a request!
    <?php
    require 'vendor/autoload.php';
    use Google\AdsApi\Common\OAuth2TokenBuilder;
    use Google\AdsApi\Dfp\DfpServices;
    use Google\AdsApi\Dfp\DfpSession;
    use Google\AdsApi\Dfp\DfpSessionBuilder;
    use Google\AdsApi\Dfp\v201708\NetworkService;
    
    // Generate a refreshable OAuth2 credential for authentication.
    $oAuth2Credential = (new OAuth2TokenBuilder())
        ->fromFile()
        ->build();
    // Construct an API session configured from a properties file and the OAuth2
    // credentials above.
    $session = (new DfpSessionBuilder())
        ->fromFile()
        ->withOAuth2Credential($oAuth2Credential)
        ->build();
    
    // Get a service.
    $dfpServices = new DfpServices();
    $networkService = $dfpServices->get($session, NetworkService::class);
    
    // Make a request
    $network = $networkService->getCurrentNetwork();
    printf(
        "Network with code %d and display name '%s' was found.\n",
        $network->getNetworkCode(),
        $network->getDisplayName()
    );
    

.NET

Here is a basic example that shows how to use the .NET client library

  1. Create a new project

    Open Visual Studio and create a new project (i.e. Console Application).

  2. Add required library references to your project

    If you're using NuGet, simply add a dependency for Google.Dfp, or Google.Dfp.Examples.CSharp if you want to run code examples.

    Otherwise, download the binary distribution from GitHub and copy all dlls from the \lib folder into your project. Add references to these dlls and to System.Web.Services in your project.

  3. Setup your App.config

    Copy src\App.config to your project directory and add it to your project. If your application has its own App.config, then you can copy the following nodes into your App.config:

    • configuration/DfpApi
    • configuration/system.web
    • configuration/configSections/section[name="DfpApi"]
    • configuration/system.net
  4. Setup credentials

    Open App.config and edit the following keys:

    <add key="ApplicationName" value="INSERT_YOUR_APPLICATION_NAME_HERE" />
    <add key="NetworkCode" value="INSERT_YOUR_NETWORK_CODE_HERE" />
    <add key="OAuth2Mode" value="SERVICE_ACCOUNT" />
    <add key="OAuth2SecretsJsonPath" value="INSERT_OAUTH2_SECRETS_JSON_FILE_PATH_HERE" />
    
  5. Make a call to the library

    You can call the library as shown in the following C# code snippet

    DfpUser user = new DfpUser();
    InventoryService inventoryService =
        (InventoryService) dfpUser.GetService(DfpService.v201705.InventoryService);
    
    // Create a statement to select ad units.
    int pageSize = StatementBuilder.SUGGESTED_PAGE_LIMIT;
    StatementBuilder statementBuilder = new StatementBuilder()
        .OrderBy("id ASC")
        .Limit(pageSize);
    
    // Retrieve a small amount of ad units at a time, paging through until all
    // ad units have been retrieved.
    int totalResultSetSize = 0;
    do {
      AdUnitPage page = inventoryService.getAdUnitsByStatement(
          statementBuilder.ToStatement());
    
      // Print out some information for each ad unit.
      if (page.results != null) {
        totalResultSetSize = page.totalResultSetSize;
        int i = page.startIndex;
        foreach (AdUnit adUnit in page.results) {
          Console.WriteLine(
              "{0}) Ad unit with ID \"{1}\" and name \"{2}\" was found.",
              i++,
              adUnit.id,
              adUnit.name
          );
        }
      }
    
      statementBuilder.IncreaseOffsetBy(pageSize);
    } while (statementBuilder.GetOffset() < totalResultSetSize);
    
    Console.WriteLine("Number of results found: {0}", totalResultSetSize);
    

If you don't want to set your credentials in your App.config, then refer to this wiki article for alternate ways of using the DfpUser class. For more detailed information about using the .NET Client Library, refer to the README . If you want to develop in .NET without the client library, please refer to the NoClientLibrary wiki article.

Ruby

Here is a basic example that shows how to use the Ruby client library. The Ruby client library requires Ruby 2.1 or later.

  1. Install the Ruby gem and get the configuration file.

    Run the following commands in a shell:

    gem install google-dfp-api
    curl https://raw.githubusercontent.com/googleads/google-api-ads-ruby/master/dfp_api/dfp_api.yml -o ~/dfp_api.yml
    
  2. Setup your credentials

    Populate the required fields in the ~/dfp_api.yml file. If you don't already have an OAuth2 keyfile, you'll need to follow the steps to create your OAuth2 credentials.

    :authentication:
      :oauth2_keyfile: INSERT_PATH_TO_JSON_KEY_FILE_HERE
      :application_name: INSERT_APPLICATION_NAME_HERE
      :network_code: INSERT_NETWORK_CODE_HERE
    
  3. Write some code and make a request!
    # Import the library.
    require 'dfp_api'
    
    # Initialize a dfp client instance (uses credentials in ~/dfp_api.yml by default).
    dfp = DfpApi::Api.new
    
    # Get a service instance.
    network_service = dfp.service(:NetworkService, :v201708)
    
    # Make a request.
    network = network_service.get_current_network()
    
    puts "The current network is %s (%d)." %
            [network[:display_name], network[:network_code]]
    

More detailed steps for getting started can be found in the README file that is distributed with the Ruby client library. Also, check out our full example library for Ruby.

Next Steps

When you have got a client library up and running, go ahead and play with the examples provided to extend them for your needs.

Visit the reference documentation to learn more about the API.

If you need help, read the advanced topics listed under the Guides section or visit the Forum.

Feedback geben zu...

DoubleClick for Publishers
DoubleClick for Publishers