LoginSignup
6
4

More than 5 years have passed since last update.

RubyでAdWords APIを実行するときはadwords_api.ymlに注意

Last updated at Posted at 2019-04-17

RubyでさくっとAdWords APIをたたいてみようかと思ったら、思わぬところではまったので、共有します。

大まかなRubyでAdWords APIを利用するまでの手順はこんな感じです。

  1. gemのgoogle-adwords-apiをインストールする
  2. 設定ファイルのadwords_api.ymlを用意する
  3. サービス等の処理が書いてある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_tokenoauth2_tokenの下の階層が正しいです。
ちなみに、expires_atoauth2_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で一通り試してみる

6
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4