在 API 要求中設定選項欄位時,您必須直接建構要求訊息物件,並將其做為單一 request 參數傳遞至服務方法。
為方便管理關鍵字參數,扁平化方法簽章會省略選用要求欄位 (例如 GoogleAdsService.Search 方法中的 validate_only 欄位),因此您必須直接在要求訊息例項中設定這些欄位。
如要判斷要求物件欄位是必要還是選用,請參閱 Google Ads API protobuf 服務定義,並尋找包含 [(google.api.field_behavior) = REQUIRED] 註解的欄位。沒有這個註解的欄位為選填。
以下範例會建構 SearchGoogleAdsRequest 物件,並在 GoogleAdsService.Search 要求中設定選用的 validate_only 欄位:
from google.ads.googleads.client import GoogleAdsClient
client = GoogleAdsClient.load_from_storage()
googleads_service = client.get_service("GoogleAdsService")
# Initialize the request object and set required fields.
request = client.get_type("SearchGoogleAdsRequest")
request.customer_id = customer_id
request.query = query
# Set the optional validate_only field.
request.validate_only = True
response = googleads_service.search(request=request)