在 Python 中擷取使用 API 所需的所有各種 proto 類別的參照,可能會很冗長,而且您必須對 API 有深入瞭解,或經常切換環境來參照 proto 或文件。
用戶端的 get_service 和 get_type 方法
這兩種 getter 方法可讓您在 API 中擷取任何服務或型別物件。get_service 方法用於擷取服務用戶端。get_type 用於任何其他物件。服務用戶端類別是在版本路徑 google/ads/googleads/v*/services/ 下的程式碼中定義,所有型別則是在各種物件類別 google/ads/googleads/v*/common|enums|errors|resources|services/types/ 下定義。版本目錄下的所有程式碼都會產生,因此建議使用這些方法,而不是直接匯入物件,以免程式碼庫的結構發生變化。
以下範例說明如何使用 get_service 方法,擷取 GoogleAdsService 用戶端 (或 google-ads v28.4.0 和後續版本中的非同步 GoogleAdsServiceAsyncClient,方法是傳遞 is_async=True) 的例項:
from google.ads.googleads.client import GoogleAdsClient
# "load_from_storage" loads your API credentials from disk so they
# can be used for service initialization. Providing the optional `version`
# parameter means that the v25 version of GoogleAdsService will
# be returned.
client = GoogleAdsClient.load_from_storage(version="v25")
googleads_service = client.get_service("GoogleAdsService")
# Supported in google-ads v28.4.0 and later: retrieve an async service client.
googleads_async_service = client.get_service("GoogleAdsService", is_async=True)
以下範例說明如何使用 get_type 方法擷取 Campaign 執行個體:
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage(version="v25")
campaign = client.get_type("Campaign")
列舉
您可以使用 get_type 方法擷取列舉,但每個 GoogleAdsClient 執行個體也有 enums 屬性,可使用與 get_type 方法相同的機制動態載入列舉。相較於使用 get_type,這個介面更簡單,也更容易讀取:
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage(version="v25")
campaign = client.get_type("Campaign")
campaign.status = client.enums.CampaignStatusEnum.PAUSED
Python 會以內建的 enum 型別表示列舉的 Proto 物件欄位。也就是說,您可以直接讀取成員的值。在 Python REPL 中使用上一個範例的 campaign 例項:
>>> print(campaign.status)
CampaignStatus.PAUSED
>>> type(campaign.status)
<enum 'CampaignStatus'>
>>> print(campaign.status.value)
3
有時瞭解對應列舉值的欄位名稱很有用。您可以使用 name 屬性存取這項資訊:
>>> print(campaign.status.name)
'PAUSED'
>>> type(campaign.status.name)
<class 'str'>
與列舉互動的方式會因 use_proto_plus 設定為 true 或 false 而異。如要瞭解這兩個介面的詳細資料,請參閱 protobuf 訊息說明文件。
版本管理
系統會同時維護多個 API 版本。雖然「v25」是最新版本,但舊版仍可存取,直到服務終止為止。程式庫包含對應各個有效 API 版本的獨立 Proto 訊息類別。如要存取特定版本的訊息類別,請在初始化用戶端時提供 version 關鍵字參數,確保系統一律從該版本傳回例項:
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage(version="v25")
# The Campaign instance will be from the v25 version of the API.
campaign = client.get_type("Campaign")
如果在初始化用戶端時未指定 version,您可以在呼叫 get_service 和 get_type 方法時,為每次呼叫指定版本 (請注意,如果在初始化 GoogleAdsClient 時設定 version,系統會覆寫傳遞至 get_service 或 get_type 的任何 version 引數):
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage()
# This loads the v25 version of the GoogleAdsService.
googleads_service = client.get_service(
"GoogleAdsService", version="v25"
)
# This loads a specific supported API version (such as v23) of a Campaign.
campaign = client.get_type("Campaign", version="v23")
如果未提供 version 關鍵字參數,程式庫預設會使用已安裝 google-ads 套件支援的最高 API 版本 (最新版本為 "v25")。請注意,次要 API 版本 (例如 v25.1) 是使用主要版本字串 (version="v25") 存取。如要查看最新版本和其他可用版本的更新清單,請前往 API 參考資料說明文件的左側導覽部分。