|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2020 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 | +"""Demonstrates how to reject or unlink a Merchant Center link request. |
| 16 | +
|
| 17 | +Prerequisite: You need to have access to a Merchant Center account. You can find |
| 18 | +instructions to create a Merchant Center account here: |
| 19 | +https://support.google.com/merchants/answer/188924. |
| 20 | +
|
| 21 | +To run this example, you must use the Merchant Center UI or the Content API for |
| 22 | +Shopping to send a link request between your Merchant Center and Google Ads |
| 23 | +accounts. You can find detailed instructions to link your Merchant Center and |
| 24 | +Google Ads accounts here: https://support.google.com/merchants/answer/6159060. |
| 25 | +""" |
| 26 | + |
| 27 | +import argparse |
| 28 | +import sys |
| 29 | + |
| 30 | +from google.ads.google_ads.client import GoogleAdsClient |
| 31 | +from google.ads.google_ads.errors import GoogleAdsException |
| 32 | + |
| 33 | + |
| 34 | +def main(client, customer_id, merchant_center_account_id): |
| 35 | + """Demonstrates how to reject a Merchant Center link request. |
| 36 | +
|
| 37 | + Args: |
| 38 | + client: An initialized Google Ads client. |
| 39 | + customer_id: The Google Ads customer ID. |
| 40 | + merchant_center_account_id: The Merchant Center account ID for the |
| 41 | + account requesting to link. |
| 42 | + """ |
| 43 | + # Get the MerchantCenterLinkService client. |
| 44 | + merchant_center_link_service = client.get_service( |
| 45 | + "MerchantCenterLinkService", version="v6" |
| 46 | + ) |
| 47 | + try: |
| 48 | + # Get the extant customer account to Merchant Center account links. |
| 49 | + list_merchant_center_links_response = merchant_center_link_service.list_merchant_center_links( |
| 50 | + customer_id |
| 51 | + ) |
| 52 | + |
| 53 | + number_of_links = len( |
| 54 | + list_merchant_center_links_response.merchant_center_links |
| 55 | + ) |
| 56 | + |
| 57 | + if number_of_links <= 0: |
| 58 | + print( |
| 59 | + "There are no current merchant center links to Google Ads " |
| 60 | + f"account {customer_id}. This example will now exit." |
| 61 | + ) |
| 62 | + return |
| 63 | + |
| 64 | + print( |
| 65 | + f"{number_of_links} Merchant Center link(s) found with the " |
| 66 | + "following details:" |
| 67 | + ) |
| 68 | + |
| 69 | + merchant_center_link_status_enum = client.get_type( |
| 70 | + "MerchantCenterLinkStatusEnum", version="v6" |
| 71 | + ).MerchantCenterLinkStatus |
| 72 | + |
| 73 | + for merchant_center_link in list_merchant_center_links_response.merchant_center_links: |
| 74 | + print( |
| 75 | + f"\tLink '{merchant_center_link.resource_name}' has status " |
| 76 | + f"'{merchant_center_link_status_enum.Name(merchant_center_link.status)}'." |
| 77 | + ) |
| 78 | + |
| 79 | + # Check if this is the link to the target Merchant Center account. |
| 80 | + if merchant_center_link.id == merchant_center_account_id: |
| 81 | + # A Merchant Center link can be pending or enabled; in both |
| 82 | + # cases, we reject it by removing the link. |
| 83 | + _remove_merchant_center_link( |
| 84 | + client, |
| 85 | + merchant_center_link_service, |
| 86 | + customer_id, |
| 87 | + merchant_center_link, |
| 88 | + ) |
| 89 | + |
| 90 | + # We can terminate early since this example concerns only one |
| 91 | + # Google Ads account to Merchant Center account link. |
| 92 | + return |
| 93 | + |
| 94 | + # Raise an exception if no matching Merchant Center link was found. |
| 95 | + raise ValueError( |
| 96 | + "No link could was found between Google Ads account " |
| 97 | + f"{customer_id} and Merchant Center account " |
| 98 | + f"{merchant_center_account_id}." |
| 99 | + ) |
| 100 | + |
| 101 | + except GoogleAdsException as ex: |
| 102 | + print( |
| 103 | + f'Request with ID "{ex.request_id}" failed with status ' |
| 104 | + f'"{ex.error.code().name}" and includes the following errors:' |
| 105 | + ) |
| 106 | + for error in ex.failure.errors: |
| 107 | + print(f'\tError with message "{error.message}".') |
| 108 | + if error.location: |
| 109 | + for field_path_element in error.location.field_path_elements: |
| 110 | + print(f"\t\tOn field: {field_path_element.field_name}") |
| 111 | + sys.exit(1) |
| 112 | + |
| 113 | + |
| 114 | +def _remove_merchant_center_link( |
| 115 | + client, merchant_center_link_service, customer_id, merchant_center_link |
| 116 | +): |
| 117 | + """Removes a Merchant Center link from a Google Ads client customer account. |
| 118 | +
|
| 119 | + Args: |
| 120 | + client: An initialized Google Ads client. |
| 121 | + merchant_center_link_service: An initialized |
| 122 | + MerchantCenterLinkService client. |
| 123 | + customer_id: The Google Ads customer ID of the account that has the link |
| 124 | + request. |
| 125 | + merchant_center_link: The MerchantCenterLink object to remove. |
| 126 | + """ |
| 127 | + # Create a single remove operation, specifying the Merchant Center link |
| 128 | + # resource name. |
| 129 | + operation = client.get_type("MerchantCenterLinkOperation", version="v6") |
| 130 | + operation.remove = merchant_center_link.resource_name |
| 131 | + |
| 132 | + # Send the operation in a mutate request. |
| 133 | + response = merchant_center_link_service.mutate_merchant_center_link( |
| 134 | + customer_id, operation |
| 135 | + ) |
| 136 | + print( |
| 137 | + "Removed Merchant Center link with resource name " |
| 138 | + f"'{response.result.resource_name}'." |
| 139 | + ) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 144 | + # home directory if none is specified. |
| 145 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 146 | + |
| 147 | + parser = argparse.ArgumentParser( |
| 148 | + description=( |
| 149 | + "Demonstrates how to reject a Merchant Center link request." |
| 150 | + ) |
| 151 | + ) |
| 152 | + # The following argument(s) should be provided to run the example. |
| 153 | + parser.add_argument( |
| 154 | + "-c", |
| 155 | + "--customer_id", |
| 156 | + type=str, |
| 157 | + required=True, |
| 158 | + help="The Google Ads customer ID.", |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "-m", |
| 162 | + "--merchant_center_account_id", |
| 163 | + type=int, |
| 164 | + required=True, |
| 165 | + help="The Merchant Center account ID for the account requesting to " |
| 166 | + "link.", |
| 167 | + ) |
| 168 | + args = parser.parse_args() |
| 169 | + |
| 170 | + main(google_ads_client, args.customer_id, args.merchant_center_account_id) |
0 commit comments