/
approve_merchant_center_link.py
169 lines (147 loc) · 6.08 KB
/
approve_merchant_center_link.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Demonstrates how to approve a Merchant Center link request.
Prerequisite: You need to have access to a Merchant Center account. You can find
instructions to create a Merchant Center account here:
https://support.google.com/merchants/answer/188924.
To run this code example, you must use the Merchant Center UI or the Content
API for Shopping to send a link request between your Merchant Center and Google
Ads accounts.
"""
import argparse
import sys
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
from google.api_core import protobuf_helpers
def main(client, customer_id, merchant_center_account_id):
"""Demonstrates how to approve a Merchant Center link request.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The client customer ID string.
merchant_center_account_id: ID of the Merchant center whose link request
is to be approved.
"""
merchant_center_link_service = client.get_service(
"MerchantCenterLinkService", version="v6"
)
try:
# [START approve_merchant_center_link]
# Retrieve all the existing Merchant Center links.
response = merchant_center_link_service.list_merchant_center_links(
customer_id
)
print(
f"{len(response.merchant_center_links)} Merchant Center link(s) "
"found with the following details:"
)
# [END approve_merchant_center_link]
merchant_center_link_status_enum = client.get_type(
"MerchantCenterLinkStatusEnum", version="v6"
).MerchantCenterLinkStatus
# Iterate through the results and filter for links with pending states.
for merchant_center_link in response.merchant_center_links:
# [START approve_merchant_center_link_1]
print(
f"Link '{merchant_center_link.resource_name}' has status "
f"'{merchant_center_link_status_enum.Name(merchant_center_link.status)}'."
)
# [END approve_merchant_center_link_1]
if (
merchant_center_link.status
== merchant_center_link_status_enum.PENDING
and str(merchant_center_link.id) == merchant_center_account_id
):
_update_merchant_center_link_status(
client,
customer_id,
merchant_center_link_service,
merchant_center_link,
merchant_center_link_status_enum.ENABLED,
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status'
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
# [START approve_merchant_center_link_2]
def _update_merchant_center_link_status(
client,
customer_id,
merchant_center_link_service,
merchant_center_link,
status,
):
"""Updates the status of a Merchant Center link request.
Args:
client: An initialized GoogleAdsClient instance.
customer_id: The client customer ID string.
merchant_center_link_service: A merchant center link service instance.
merchant_center_link: The merchant center link to be modified.
status: The updated status to apply to the merchant center link.
"""
# Creates an operation.
operation = client.get_type("MerchantCenterLinkOperation", version="v6")
link_to_update = operation.update
link_to_update.resource_name = merchant_center_link.resource_name
# Enables the pending link.
link_to_update.status = status
field_mask = protobuf_helpers.field_mask(None, link_to_update)
operation.update_mask.CopyFrom(field_mask)
# Updates the link.
mutate_response = merchant_center_link_service.mutate_merchant_center_link(
customer_id, operation
)
merchant_center_link_status_enum = client.get_type(
"MerchantCenterLinkStatusEnum", version="v6"
).MerchantCenterLinkStatus
# Displays the result.
print(
"The status of Merchant Center Link with resource name "
f"'{mutate_response.result.resource_name}' to Google Ads account : "
f"{customer_id} was updated to "
f"{merchant_center_link_status_enum.Name(status)}."
)
# [END approve_merchant_center_link_2]
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage()
parser = argparse.ArgumentParser(
description=("Approves a Merchant Center link request.")
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-m",
"--merchant_center_account_id",
type=str,
required=True,
help="ID of the Merchant Center account whose link request is to be "
"approved.",
)
args = parser.parse_args()
main(google_ads_client, args.customer_id, args.merchant_center_account_id)