LoginSignup
6
3

More than 1 year has passed since last update.

【python】google-ads v8.2.0 → v14.0.0 にアップデートの際に変更した箇所

Last updated at Posted at 2021-09-15

1. 概要

 google-ads ライブラリを使用してAPIを叩いて広告配信結果(kpi)を取得するバッチ処理を実行していましたが次のようなエラーを吐くようになりました。

Request made: ClientCustomerId: xxxxxxxxx, Host: googleads.googleapis.com:443, 
Method: /google.ads.googleads.v6.services.GoogleAdsService/Search, 
RequestId: yyyyyyyyyyyy, IsFault: True, 
FaultMessage:  Version v6 is deprecated. Requests to this version will be blocked.
Request with ID "yyyyyyyyyyyy" failed with status "INVALID_ARGUMENT" and includes the following errors:
Error with message " Version v6 is deprecated. Requests to this version will be blocked.".

 要はバージョンをv6を使用するなということですね。現時点でGoogle Ads API のバージョンはv6, v7, v8がありv8が推奨されているようです。
google-ads==8.2.0 を使用していましたが、このバージョンではv8を叩けないので、最新の14.0.0にアップグレードしました。その結果、コードを何点か修正する必要がありましたので、ここに記しておきます。

2. 目次

3. 手順

  • google-ads==14.0.0にアップグレード
  • コード修正
    • インポート先変更
    • GoogleAdsClient の初期化にパラメータを追加
    • kpiを取得する際の引数変更

3.1. google-ads==14.0.0にアップグレード

次のコマンドでアップグレードします

pip install --upgrade google-ads==14.0.0

3.2. コード修正

3.2.1. インポート先変更

次のようにインポート先を変更します

# from google.ads.google_ads.client import GoogleAdsClient
from google.ads.googleads.client import GoogleAdsClient

3.2.2. GoogleAdsClient の初期化にキーを追加

アップグレード後、コードを動かすとGoogleAdsClientの初期化の時点で次のエラーを吐きました。

ValueError: The client library configuration is missing the required "use_proto_plus" key. 
Please set this option to either "True" or "False". 
For more information about this option see the Protobuf Messages guide: https://developers.google.com/google-ads/api/docs/client-libs/python/protobuf-messages

use_proto_plus というキーを追加する必要があるようですね。指示されたドキュメントを読むと、既存のコードを変更したくない場合は True を設定とのことでした。私のケースでは、yamlからパラメータ設定をしているので、次のように追記しました。

developer_token: xxxxxxxxxxxxxxxxxx
client_id: xxxxxxxxxxxxxxxxxx
client_secret: xxxxxxxxxxxxxxxxxx
refresh_token: xxxxxxxxxxxxxxxxxx
use_proto_plus: True

3.3.3. kpiを取得する際の引数変更

kpiを取得する search() の部分でエラーを出しました。

results = self.ga_service.search(self.customer_id, query, PAGE_SIZE)
TypeError: search() takes from 1 to 2 positional arguments but 4 were given

ライブラリのソースコードを見てみると引数の指定方法が変わっていました。位置引数になっていましたね。
改めてドキュメントを見てみると次のようなコードでしたので、丸々そちらに変更しました。

search_request = client.get_type("SearchGoogleAdsRequest")
search_request.customer_id = customer_id
search_request.query = query
search_request.page_size = page_size

results = ga_service.search(request=search_request)

4. まとめ

上記3つを修正すると、無事バッチ処理は動きました。思ったよりも修正箇所が多かったです。

参考

https://github.com/googleads/google-ads-python
https://developers.google.com/google-ads/api/docs/client-libs/python

6
3
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
6
3