RubyでさくっとAdWords APIをたたいてみようかと思ったら、思わぬところではまったので、共有します。
大まかなRubyでAdWords APIを利用するまでの手順はこんな感じです。
- gemの
google-adwords-api
をインストールする - 設定ファイルの
adwords_api.yml
を用意する - サービス等の処理が書いてあるrbファイルを実行する
この中で2.のadwords_api.yml
を用意する際に注意が必要です。
公式のリファレンスの中でadwords_api.yml
のサンプルはこのようになっています。(必要な部分のみ抜粋)
[...]
:oauth2_client_id: xxxxxxxxxx.apps.googleusercontent.com
:oauth2_client_secret: zZxxxxxTxxxxxxxxxxx
:refresh_token: 1/dyOIp7ki-xxxxxxxxxxxxxxxxxxxxxxxx
:developer_token: 123axxxxxxxxxxxxxxxxxx
[...]
しかし、この通りに設定してもうまく動きません。
実際に動く形はこう。
:authentication:
:oauth2_client_id: xxxxxxxxxx.apps.googleusercontent.com
:oauth2_client_secret: zZxxxxxTxxxxxxxxxxx
:oauth2_token:
:refresh_token: 1/dyOIp7ki-xxxxxxxxxxxxxxxxxxxxxxxx
:developer_token: 123axxxxxxxxxxxxxxxxxx
refresh_token
はoauth2_token
の下の階層が正しいです。
ちなみに、expires_at
もoauth2_token
の下になります。
おまけ
adwords_api.yml
は$HOME
をデフォルトで探しに行きますが、API実行時に動的にパスを指定することもできます。
同じ階層にある場合
require "adwords_api"
config_filename = File.join(Dir.getwd, "adwords_api.yml")
adwords = AdwordsApi::Api.new(config_filename)
managed_customer_srv = adwords.service(:ManagedCustomerService, :v201809)
参考
詳しい手順や情報はこちらの記事を参照。
最初の API 呼び出しを実行する | AdWords API | Google Developers
Home · googleads/google-api-ads-ruby Wiki
AdWords APIをRubyで一通り試してみる