LoginSignup
23
10

More than 5 years have passed since last update.

AdWords APIをRubyで一通り試してみる

Last updated at Posted at 2016-11-11

一読した方が良い資料

AdWords APIの基本概念
https://developers.google.com/adwords/api/docs/guides/basic-concepts?hl=ja
Adwords APIで利用されるプロトコルや、
APIにて取得、更新するオブジェクトについて記載されている。

Oauth2の仕組み
https://murashun.jp/blog/20150920-01.html
この資料自体、30分程度で読めるので読んだほうが良い。
どのようなパラメータがなぜ必要なのか把握できるため、
問題が起きたときのデバックに便利

Ouath2認証用ライブラリ
https://github.com/google/signet
Oauth2認証をしているライブラリ。
AdWords APIで使うパラメータとOauth2の各種要素の対応付けを、
把握したければ読んだ方が良い。

Adwords API用ライブラリ
https://github.com/googleads/google-api-ads-ruby
今回使う AdWords API用ライブラリ。
AdWords API用のエンドポイント設定や、リクエスト用XMLの作成、
レスポンスの整形とかしてくれる。

使うまでの手順

  • AdWords API用開発者トークンの取得
  • Developer ConsoleにてOauth2クライアントを作成
  • Oauth2認証をしてrefresh tokenを取得する
  • テストで叩いてみる

AdWords API用開発者トークンの取得

https://developers.google.com/adwords/api/docs/signingup?hl=ja
基本的にこの手順に従って取得すればOK.
取得した後は、adwords管理画面にて、「アカウント設定」=> 「AdWords API センター」
と遷移すればいつでも確認できる。
なお、テスト用アカウントで動作確認する際も、この開発者用トークンが必要なので要注意

Developer ConsoleからOauth2クライアントを生成

https://developers.google.com/adwords/api/docs/guides/authentication?hl=ja
ウェブサイトに対するコールバックが必要でなかったので、
インストール型アプリケーションで認証情報を作った。
ここで作られた client_idとclient_secretはちゃんと確保しておくこと。

なお、他にもサービスアカウントを使う方法もあるが、下記の理由によって却下した。

ドメイン限定の機能(成り代わり機能など)が必要な場合を除き、
サービス アカウントではなく、オフライン フローかウェブ フローの
どちらかを使うことを強くおすすめします。

Oauth2認証をする

インストール型アプリケーションで認証するなら、下記URLの手順通りに行えば大丈夫
https://github.com/googleads/google-api-ads-ruby/wiki/API-access-using-own-credentials-%28installed-application-flow%29
諸々URL切れてたりするので、一応自分がやった手順を書いていく

Oauth2認証時に必要なパラメータ

Oauth2で認証するためのscriptは下記のようになっている。
https://github.com/googleads/google-api-ads-ruby/blob/master/adwords_api/examples/v201609/misc/setup_oauth2.rb

認証をする際には、adwords_api.ymlを読み込む必要がある。
なお、読み込むPATHはデフォルトだと$HOME。

#adwords_api.yml
:authentication:
  :method: OAuth2
  :oauth2_client_id: xxx
  :oauth2_client_secret: xxxx
  :developer_token: xxx
  :client_customer_id: xxxx
  :oauth2_access_type: offline

認証scriptを実行すると、adwords_api.ymlにoauth2認証用のパラメータが設定される。

#adwords_api.yml
  :oauth2_token:
    :access_token: option
    :refresh_token: xxxxx
    :issued_at: 2016-11-11 11:11:26.379096000 +09:00
    :expires_in: 3600
    :id_token:

これらのパラメータがなぜ必要なのかは、signetのライブラリを読むと把握できる。
なお、refresh tokenがあってexpireが切れてると、access_tokenは再取得してくれる。

テストscriptを実行する

require 'adwords_api'
API_VERSION = :v201609

config_filename = File.join('path_to_config', 'adwords_api.yml')
adwords = AdwordsApi::Api.new(config_filename)

selector = {
  :fields => ['Id', 'Name', 'Status'],
  :ordering => [
    {:field => 'Name', :sort_order => 'ASCENDING'}
  ],
  :paging => {
    :start_index => 0,
    :number_results => 5
  }
}

campaign_srv = adwords.service(:CampaignService, API_VERSION)
arr = campaign_srv.get(selector)

上記scriptを実行すると、下記のような結果が返ってくる.

% be rails runner script/test_adwords_api.rb                                                                                                                              [11:21:23]
I, [2016-11-11T11:30:29.549446 #47904]  INFO -- sentry: ** [Raven] Raven 1.2.0 configured not to send errors.
{:total_num_entries=>51, :page_type=>"CampaignPage", :entries=>[{:id=>xxx, :name=>"xxxxx", :status=>"PAUSED", :budget=>nil}, {:id=>xxx, :name=>"xxx", :status=>"PAUSED", :budget=>nil}, :status=>"PAUSED", :budget=>nil}]}

最後に

以上、AdWords APIを一通り試す手順を記載してみた。
誤り等あれば直しますので、ご指摘おなしゃす :pray:

23
10
0

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
23
10