【Google Ads API】Google APIからキーワードアイデア取得【Python】

Google広告にAPIがあることを知ってるでしょうか?かなーーーり手順としては面倒ですがキーワードリストを取得できたりして面白いことができそうな予感です。
今回はGoogle Ads APIを使ってGoogleが提供しているサンプルスクリプト(Python)でキーワードアイデアを取得してみます。
必要なアカウントとアクセスキー一覧
必要なアカウントと取得するアクセスキーの関係を表にしておきます。 APIに「テスト」用と、「プロダクション」用、があるのですが「プロダクション用」は申請受理のハードルがすごく高いので今回はテストアカウントで利用します。
アカウント | アクセスキー | コメント |
---|---|---|
Googleアカウント | Google広告を利用できるアカウント | |
MCCアカウント | developer_token | クライアント センター(MCC)アカウントは、複数のGoogle広告アカウント管理のためのアカウント。API開発者トークン取得 |
APIプロジェクトアクセス用アカウント | client_id , client_secret | スクリプトからアクセスするアカウント。クライアントキー取得 |
refresh_token | キーのリフレッシュのためのトークン。スクリプトを実行して取得 | |
テストアカウント | login_customer_id | MCCアカウントの一種でテスト用。スクリプトのログイン先id |
なんでこんなにややこしい体系なのか理解に苦しみます。。
Google広告アカウント作成
Google広告にアクセスして作成します。
Google広告
初回に必ずキャンペーン(Googleの検索結果に広告を載せる設定)を作る必要があります。
Google広告キャンペーン
アカウントの作成が終わったら、キャンペーンは消しておきましょう。
MCCアカウント作成
Google広告のログイン前のトップ画面の「高度な機能」に「クライアントセンター(MCC)」というのがあるのでクリックして進みます。
Google広告:MCC
次の画面で「クライアントセンター(MCC)を作成」でアカウントを作成してください。
Google広告:MCC作成
MCCアカウントからのAPI開発者トークン取得
作成したMCCアカウントからAPI開発者トークンを生成します。
メイン画面の左上が先程作成したMCCアカウントになっている状態で「ツールと設定」→「APIセンター」を選びます。
Google広告:MCC-API開発者トークン作成
適当に設定して開発者申請をすると以下の画面のようになります。
Google広告:MCC-API開発者トークン
この開発者トークン(developer_token)は後ほど使うので記録しておいて下さい。今回はアクセス権を「テストアカウント」にしています。作ったアプリを公開したりする場合はプロダクション用に「ベーシックアクセスを申請」してください。
APIプロジェクトの作成とOAuthクライアント作成
いよいよAPIを使ったプロジェクトを作成します。プロジェクトはGoogle API コンソールで管理されます。まず、Google APIコンソールの「API ライブラリ」にアクセスして、「Google Ads API」を検索します。
Google APIコンソール: API検索
Google APIコンソール: API選択
Google Ads APIを有効化します。
Google APIコンソール: API有効化
有効化したAPIをどのプロジェクトと関連付けるか聞かれますが、まだプロジェクトを作っていないので新規作成をクリックしてプロジェクトを作成します。
Google APIコンソール: プロジェクト作成
プロジェクト名を適当につけて作成しましょう。
Google APIコンソール: プロジェクト設定
メニューから「ライブラリ」を選んで再度「Google Ads API」を選びます。
Google APIコンソール: ライブラリメニュー
上部に作成したプロジェクト名が入っていることを確認して、Google Ads APIを有効化します。
Google APIコンソール: Ads API有効化
下の画面のようになります。次に「認証情報(client_id , client_secret|)」を作成しますが、下の図の左側のメニューから「認証情報」選んで作成して下さい。
Google APIコンソール: 認証情報作成
認証情報画面では、中央上部の「+認証情報を作成」のメニューから「OAuthクライアントID」を選択します。
Google APIコンソール: OAuthクライント作成
OAuthクライアント作成画面では先に「同意画面」を作成しろと言われるので従います。
Google APIコンソール: OAuthクライント同意画面作成
同意画面は「外部」を選び、次の画面では最低限「アプリケーション名」を入れましょう。
同意画面が作成できたら再び「認証情報」から「OAuthクライアントID」の作成を行います。
今回のアプリケーションはPythonスクリプトなので「その他」を選びます。
Google APIコンソール: OAuthクライント-その他
OAuthクライアントが作成されると、下の画面のように「OAuth」のところにユーザーが出来上がっているので、右端の矢印から「client_secretxxxxxxx.json」ファイルをダウンロードして下さい。
Google APIコンソール: OAuthクライント-キー取得
テストアカウント作成
MCCのテスト用のアカウントを作成します。
Google 広告 MCC アカウントのページに移動してアカウントを作成するとテストアカウントができます。
テストアカウントは右上のアイコンの横に赤い箱が表示されます。
Google MCC テストアカウント
テストアカウントができたらアカウント名の横にある数字「xxx-xxx-xxxx」を記録しておいて下さい(login_customer_id)
Google Ads ライブラリのインストール
Google Ads向けにPythonライブラリが用意されているのでインストールします。(Python3.7以上)
pip3 install google-ads
サンプルスクリプトもダウンロードしましょう。
GitHub - googleads/google-ads-python: Google Ads API Client Library for Python
この中で今回使うサンプルファイルは以下のものなのでダウンロードしておきましょう。
google-ads.yaml
examples/authentication/authenticate_in_standalone_application.py
examples/planning/generate_keyword_ideas.py
OAuth認証とリフレッシュトークンの取得
ここまですすんできたら足りないキーは「リフレッシュトークン」だけです。 ダウンロードしたスクリプト「authenticate_in_standalone_application.py」とOAuthクライアントのjsonファイル「client_secret.json」が必要です。
$ python3 authenticate_in_standalone_application.py --client_secrets_path ./client_secret.json
Please visit this URL to authorize this application:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=xxxxxxxxxxxxxxx&prompt=consent&access_type=offline
Enter the authorization code:

Google Ads API: OAuth合意
その後、認証コードが表示されるのでコピーして、スクリプト実行画面の「Enter the authorization code:」のところに貼り付けます。

Google Ads API: OAuth合意
すると、以下のようにリフレッシュトークンが出てくるので記録します。
Access token:
ya29.axxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Refresh token:
1//0eyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
キーワードアイデア取得スクリプトの実行
無事に5つのキーが手に入ったでしょうか?(RPGみたいですね笑) これらのキーをダウンロードした「google-ads.yaml」に埋め込んでいきます。
developer_token: xxxxxxxxxxxxxxx #開発者トークン
client_id: 149351412578-pre9tcg4rr........com #client_secret.jsonから抜き出し
client_secret: gOQHW................... #client_secret.jsonから抜き出し
refresh_token: 1//0e4.................... #リフレッシュトークン
login_customer_id: 1234253142 # テストユーザーのID(ハイフン無し)
python3 generate_keyword_ideas.py -c 1234253142 -k "ブログ"
- -c : テストユーザーのID
- -k : キーワード
実行すると以下のように結果がずらずら出てきます。
Keyword idea text "goo ブログ" has 10 average monthly searches and "LOW" competition.
Keyword idea text "yahoo ブログ" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "ブログ ameba" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "ブログ sns 違い" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "写真 ブログ ランキング" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "crooz ブログ" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "月 ブログ" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "ブログ 背景 無料" has 10 average monthly searches and "LOW" competition.
Keyword idea text "ブログ サイト おすすめ" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "ブログ ランキング 一般人" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "ブログ ログ" has 10 average monthly searches and "UNSPECIFIED" competition.
Keyword idea text "google ブログ" has 10 average monthly searches and "LOW" competition.
Keyword idea text "楽天 ブログ" has 10 average monthly searches and "LOW" competition.
Keyword idea text "はてな ブログ" has 90 average monthly searches and "LOW" competition.
Keyword idea text "アメーバ ブログ" has 110 average monthly searches and "LOW" competition.
Keyword idea text "ブログ" has 260 average monthly searches and "LOW" competition.
Keyword idea text "海老蔵 ブログ" has 880 average monthly searches and "LOW" competition.
参考情報
web サービスの概要 | Google Ads API Beta | Google Developers
web GitHub - googleads/google-ads-python: Google Ads API Client Library for Python
ちょっと敷居が高いけど、きっと面白いことができるはず!
海老蔵 ブログ、読みたい!