可选请求标头
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在请求方法中使用任何可选字段时,必须在方法外部构建请求消息,并将其作为单个参数传入。
可选的请求标头(例如 GoogleAdsService.Search
方法中的 validate_only
标头)不会以关键字参数的形式出现在方法签名中,因此必须直接在请求消息中设置。
如需确定请求对象字段是必需字段还是可选字段,您可以参考服务的 protobuf 定义,并查找包含注释 [(google.api.field_behavior) =
REQUIRED]
的字段。
以下示例展示了如何在 GoogleAdsService.Search
请求中设置可选的 validate_only
字段:
request = client.get_type("SearchGoogleAdsRequest")
request.customer_id = customer_id
request.query = query
request.validate_only = True
response = googleads_service.search(request=request)
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-27。
[null,null,["最后更新时间 (UTC):2025-08-27。"],[[["\u003cp\u003eWhen using optional fields in Google Ads API requests, construct the request message separately and pass it as a single parameter to the method.\u003c/p\u003e\n"],["\u003cp\u003eOptional request headers like \u003ccode\u003evalidate_only\u003c/code\u003e are set directly on the request message, not as keyword parameters in the method signature.\u003c/p\u003e\n"],["\u003cp\u003eYou can identify required or optional request fields by checking the protobuf definitions and looking for the \u003ccode\u003e[(google.api.field_behavior) = REQUIRED]\u003c/code\u003e annotation.\u003c/p\u003e\n"],["\u003cp\u003eTo use optional fields, create an instance of the request message type, populate its fields including the optional ones, and then pass it to the API method.\u003c/p\u003e\n"]]],[],null,["# Optional request headers\n\nWhen using any\n[optional fields](//protobuf.dev/programming-guides/proto3/#field-labels) on a\nrequest method, the request message must be constructed outside of the\nmethod and passed in as a single parameter.\n\nOptional request headers, for example the\n[`validate_only`](/google-ads/api/reference/rpc/v21/SearchGoogleAdsRequest#validate_only) header in\nthe [`GoogleAdsService.Search`](/google-ads/api/reference/rpc/v21/GoogleAdsService/Search) method,\nare not present in the method signature as keyword parameters, so they must be\nset on the request message directly.\n\nTo determine if a request object field is required or optional, you can\nreference the [protobuf definitions for\nservices](//github.com/googleapis/googleapis/tree/master/google/ads/googleads/v21/services)\nand look for fields that contain the annotation `[(google.api.field_behavior) =\nREQUIRED]`.\n\nHere's an example that sets the optional `validate_only` field\non a [`GoogleAdsService.Search`](/google-ads/api/reference/rpc/v21/GoogleAdsService/Search) request: \n\n request = client.get_type(\"SearchGoogleAdsRequest\")\n request.customer_id = customer_id\n request.query = query\n request.validate_only = True\n\n response = googleads_service.search(request=request)"]]