つかびーの技術日記

(情報)工学修士, 元SIer SE, 現Web系 SEの技術blogです。Scala, Java, JS, TS, Python, Ruby, AWS, GCPあたりが好きです。

Google DoubleClick for Publishers (DFP)のAPIを使う!API準備編

   

前回(Google DoubleClick for Publishers (DFP)のAPIを使う!基礎知識編)はDFPとは何かを説明しました。

今回はDFPのAPIを使う環境を準備します。

(めっちゃ頑張って書いたのにWordPressのバグなのか回線の問題なのか保存できてなくて俺の数時間が消失しました。。。なので簡潔に行きます)

前提

以下の環境を予め準備しておきます。

  • Java
  • Eclipse or Intellij
  • Maven

DFPアカウント取得

https://www.google.co.jp/intl/ja/doubleclick/publishers/welcome/

上記ページの右上からログインボタンを押すと登録開始できるかと思います。

分からない場合は以下のサイトなどが参考になるかと思います。

DFPスタンダードの登録方法 – アドセンス実践会

GoogleAPI設定

https://console.developers.google.com/

Google Developers Consoleへ行き、新規プロジェクト作成後にDFPI APIをONにします。

さらに新規アプリケーション作成を行いclient idやsecretを生成します。

Javaプロジェクト作成

DFPは公式のサンプルが豊富です。

https://github.com/googleads/googleads-java-lib/tree/master/examples/dfp_axis/src/main/java/dfp/axis

上記のページに飛んで適当なバージョンを選択するとサンプルが色々見れます。

まずはauthディレクトリ内にあるGetRefreshToken.javaをコピペしてきます。新規Javaプロジェクト内にこのソースを置き、maven設定を行います。pom.xmlに以下の依存性を書き、コンパイルが通るようにします。

    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>ads-lib</artifactId>
      <version>1.29.0</version>
    </dependency>
    <dependency>
      <groupId>com.google.api-ads</groupId>
      <artifactId>dfp-axis</artifactId>
      <version>1.29.0</version>
    </dependency>

最後に認証情報を表現するads.propertiesを作成します。ads.propertiesはDFP APIが標準で読み込むようになっている必須設定ファイルです。クラスパスが通る位置(プロジェクトのルートとかresourcesフォルダ)に置きます。

# Credentials to use for accessing the DFP API
# Detailed descriptions of these properties can be found at:
# https://developers.google.com/doubleclick-publishers/docs/soap_xml

# OfflineCredentials authentication properties.
# A refresh token can be acquired using the GetRefreshToken example.
api.dfp.refreshToken=INSERT_REFERSH_TOKEN_HERE
# If you do not have a client ID or secret, please create one in the
# API console: https://code.google.com/apis/console#access
api.dfp.clientId=INSERT_CLIENT_ID_HERE
api.dfp.clientSecret=INSERT_CLIENT_SECRET_HERE

# Any application name of your choosing.
api.dfp.applicationName=INSERT_APPLICATION_NAME_HERE

# The header networkCode is needed for every request except
# NetworkService.makeTestNetwork and NetworkService.getAllNetworks. Comment
# out the line below when calling either of those methods.
api.dfp.networkCode=INSERT_NETWORK_CODE_HERE

# Enable/disable automatic OAuth2 token refreshing. Default is enabled.
# api.dfp.refreshOAuth2Token=true

# Change the DFP API endpoint server. Optional.
# api.dfp.endpoint=https://www.google.com/

# DEPRECATED:
#
# ClientLogin is now deprecated. Please use OAuth2 instead. See GetRefreshToken
# example for more information.
# Either email and password or clientLoginToken should be provided
# api.dfp.email=INSERT_EMAIL_HERE
# api.dfp.password=INSERT_PASSWORD_HERE
# Optional, for cached AuthTokens.
# api.dfp.clientLoginToken=INSERT_AUTH_TOKEN_HERE

api.dfp.clientIdなどは先ほどGoogle Developers Consoleで発行したものを入れておきます。networkのidはDFP画面上で確認できるので、その値を入れておきます。

これでGetRefreshTokenを実行するとコンソールに「このURLをブラウザにペーストしろ」と出るのでペーストします。そこでGoogleの認証ボタンを押すと謎の文字列が発行されます。これをコンソールに入力してEnterを押すとRefreshTokenが発行されます。

このRefreshTokenを上記ads.propertiesに入れて準備完了です。

次回以降はAPIについて触れて行きます。

 - ツール