Doubleclick for Publishers - Java Google APIs

DoubleClick for Publishers (DFP) Small Business is a free ad management solution that helps growing publishers sell, schedule, deliver, and measure all of their digital ad inventory.

  1. Maven Dependency

    <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>
    
  2. Authentication

    Create file ads.properties in resources folder

    # Any application name of your choosing
    api.dfp.applicationName=MyApp
    # Get networkCode in https://www.google.com/dfp/
    api.dfp.networkCode=12345678
    # Either email and password or clientLoginToken should be provided
    api.dfp.email=your.email@gmail.com
    api.dfp.password=yourpassword
    

    Update: From June 2014, all DFP API calls must be authorized through OAuth 2.0. If your app using ClientLogin, please migrate to OAuth2. The example in this post using ClientLogin.

    About OAuth 2.0, see Using OAuth 2.0 to Access Google APIs

  3. Create AbstractDfpSessionService.java

    abstract class AbstractDfpSessionService {
      protected static Logger log = Logger.getLogger(AbstractDfpSessionService.class);
      protected Network getCurrentNetWork() throws Exception {
        // get DFP session
        DfpSession session = getDfpSession();
        // create DFP service
        DfpServices dfpServices = new DfpServices();
        NetworkServiceInterface networkService = dfpServices
            .get(session, NetworkServiceInterface.class);
        // get Network
        Network network = networkService.getCurrentNetwork();
        log.debug("current-network code=[" + network.getCurrencyCode() + "]");
        log.debug("display-name=[" + network.getDisplayName() + "]");
        return network;
      }
      protected User getCurrentUser() throws Exception {
        // get DFP session
        DfpSession session = getDfpSession();
        // create DFP service
        DfpServices dfpServices = new DfpServices();
        UserServiceInterface userService = dfpServices
            .get(session, UserServiceInterface.class);
        return userService.getCurrentUser();
      }
      protected abstract  T getService() throws Exception;
      protected DfpSession getDfpSession() throws Exception {
        return getDfpSession(getClientLoginToken());
      }
      private DfpSession getDfpSession(final String clientLoginToken)
          throws ConfigurationLoadException, ValidationException {
        return new DfpSession.Builder()
          .fromFile()
          .withClientLoginToken(clientLoginToken)
          .build();
      }
      private String getClientLoginToken()
          throws ConfigurationLoadException, ValidationException, IOException {
        String token = new ClientLoginTokens.Builder()
          .forApi(ClientLoginTokens.Api.DFP)
          .fromFile()
          .build()
          .requestToken();
        log.debug("ClientLoginToken=[" + token + "]");
        return token;
      }
    }
    
  4. Create DfpCompanyService.java, DfpLineItemService.java, DfpOrderService.java

    public class DfpCompanyService extends AbstractDfpSessionService {
      public final Company getCompany(Long dfpCompanyId) throws Exception {
        if (dfpCompanyId == null) {
          log.warn("DFP CompanyId cannot null");
          return null;
        }
        return getService().getCompany(dfpCompanyId);
      }
      public final Company[] findCompanyByName(String dfpName) throws Exception {
        if (dfpName == null) {
          log.warn("DFP name can't null");
          return null;
        }
        StatementBuilder sb = new StatementBuilder()
            .where("name = :dfpName")
            .withBindVariableValue("dfpName", dfpName);
        return getService().getCompaniesByStatement(sb.toStatement()).getResults();
      }
      public final Company insertCompany(Long advertiserId, String advertiserName) throws Exception {
        if (StringUtil.isBlank(advertiserName) || advertiserId == null) {
          log.warn("Advertiser Name or Advertiser Id can't null");
          return null;
        }
        Company comp = new Company();
        comp.setName(joins(advertiserId, advertiserName));
        comp.setExternalId(advertiserId.toString());
        comp.setType(CompanyType.ADVERTISER);
        return getService().createCompany(comp);
      }
      public final Company updateCompany(Long advertiserId, String advertiserName, Long dfpAdvertiserId) throws Exception {
        if (advertiserId == null || dfpAdvertiserId == null) {
          log.warn("DFP Advertiser Id or Advertiser Id can't null");
          return null;
        }
        Company comp = getCompany(dfpAdvertiserId);
        comp.setName(advertiserId + "_" + advertiserName);
        comp.setExternalId(advertiserId.toString());
        return getService().updateCompany(comp);
      }
      protected final CompanyServiceInterface getService() throws Exception {
        DfpSession session = getDfpSession();
        DfpServices dfpServices = new DfpServices();
        return dfpServices.get(session, CompanyServiceInterface.class);
      }
    }
    
  5. References

Comments

Popular posts from this blog

Reduce TIME_WAIT Socket Connections