Skip to content

Commit 6511966

Browse files
githaruoBenRKarl
authored andcommitted
The file has been moved to the appropriate directory and the suggested points have been fixed.
1 parent 43b3144 commit 6511966

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python
2+
# Copyright 2023 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""This code example adds an ad group asset.
16+
17+
To upload image assets, run misc/upload_image_asset.py.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
24+
from google.ads.googleads.client import GoogleAdsClient
25+
from google.ads.googleads.errors import GoogleAdsException
26+
from google.api_core import protobuf_helpers
27+
28+
29+
# [START add_ad_group_image_asset]
30+
def main(client, customer_id, ad_group_id, asset_id):
31+
ad_group_asset_service = client.get_service("AdGroupAssetService")
32+
ad_group_asset_resource_name = ad_group_asset_service.asset_path(
33+
customer_id, asset_id
34+
)
35+
36+
ad_group_asset_operation = client.get_type("AdGroupAssetOperation")
37+
ad_group_asset_set = ad_group_asset_operation.create
38+
ad_group_asset_set.asset = ad_group_asset_resource_name
39+
ad_group_asset_set.field_type = client.enums.AssetFieldTypeEnum.AD_IMAGE
40+
ad_group_asset_set.ad_group = ad_group_asset_service.ad_group_path(
41+
customer_id, ad_group_id
42+
)
43+
response = ad_group_asset_service.mutate_ad_group_assets(
44+
customer_id=customer_id, operations=[ad_group_asset_operation]
45+
)
46+
47+
for result in response.results:
48+
print(
49+
"Created ad group asset with resource name: "
50+
f"'{result.resource_name}'"
51+
)
52+
# [End add_ad_group_image_asset.py]
53+
54+
55+
if __name__ == "__main__":
56+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
57+
# home directory if none is specified.
58+
googleads_client = GoogleAdsClient.load_from_storage(version="v15")
59+
60+
parser = argparse.ArgumentParser(
61+
description=(
62+
"Updates an ad group for specified customer and ad group "
63+
"id with the given image asset id."
64+
)
65+
)
66+
# The following argument(s) should be provided to run the example.
67+
parser.add_argument(
68+
"-c",
69+
"--customer_id",
70+
type=str,
71+
required=True,
72+
help="The Google Ads customer ID.",
73+
)
74+
parser.add_argument(
75+
"-a", "--ad_group_id", type=str, required=True, help="The ad group ID."
76+
)
77+
parser.add_argument(
78+
"-s",
79+
"--asset_id",
80+
type=str,
81+
required=True,
82+
help="The asset ID.",
83+
)
84+
85+
try:
86+
main(
87+
googleads_client, args.customer_id, args.ad_group_id, args.asset_id
88+
)
89+
except GoogleAdsException as ex:
90+
print(
91+
f'Request with ID "{ex.request_id}" failed with status '
92+
f'"{ex.error.code().name}" and includes the following errors:'
93+
)
94+
for error in ex.failure.errors:
95+
print(f'\tError with message "{error.message}".')
96+
if error.location:
97+
for field_path_element in error.location.field_path_elements:
98+
print(f"\t\tOn field: {field_path_element.field_name}")
99+
sys.exit(1)

0 commit comments

Comments
 (0)