|
| 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 | +"""This code example sends an invitation email to a user. |
| 16 | +
|
| 17 | +The invitation is to manage a customer account with a desired access role. |
| 18 | +""" |
| 19 | + |
| 20 | + |
| 21 | +import argparse |
| 22 | +import sys |
| 23 | + |
| 24 | +from google.ads.google_ads.client import GoogleAdsClient |
| 25 | +from google.ads.google_ads.errors import GoogleAdsException |
| 26 | + |
| 27 | + |
| 28 | +def main(client, customer_id, email_address, access_role): |
| 29 | + """The main method that creates all necessary entities for the example. |
| 30 | +
|
| 31 | + Args: |
| 32 | + client: An initialized GoogleAdsClient instance. |
| 33 | + customer_id: The client customer ID str. |
| 34 | + email_address: The email address for the user receiving the invitation. |
| 35 | + access_role: The desired access role for the invitee. |
| 36 | + """ |
| 37 | + service = client.get_service( |
| 38 | + "CustomerUserAccessInvitationService", version="v6" |
| 39 | + ) |
| 40 | + # [START invite_user_with_access_role] |
| 41 | + invitation_operation = client.get_type( |
| 42 | + "CustomerUserAccessInvitationOperation", version="v6" |
| 43 | + ) |
| 44 | + invitation = invitation_operation.create |
| 45 | + invitation.email_address = email_address |
| 46 | + invitation.access_role = client.get_type( |
| 47 | + "AccessRoleEnum", version="v6" |
| 48 | + ).AccessRole.Value(access_role) |
| 49 | + try: |
| 50 | + response = service.mutate_customer_user_access_invitation( |
| 51 | + customer_id, invitation_operation |
| 52 | + ) |
| 53 | + print( |
| 54 | + "Customer user access invitation was sent for " |
| 55 | + f"customer ID: '{customer_id}', " |
| 56 | + f"email address {email_address}, and " |
| 57 | + f"access role {access_role}. The invitation resource name is: " |
| 58 | + f"{response.result.resource_name}" |
| 59 | + ) |
| 60 | + except GoogleAdsException as ex: |
| 61 | + print( |
| 62 | + f'Request with ID "{ex.request_id}" failed with status ' |
| 63 | + f'"{ex.error.code().name}" and includes the following errors:' |
| 64 | + ) |
| 65 | + for error in ex.failure.errors: |
| 66 | + print(f'\tError with message "{error.message}".') |
| 67 | + if error.location: |
| 68 | + for field_path_element in error.location.field_path_elements: |
| 69 | + print(f"\t\tOn field: {field_path_element.field_name}") |
| 70 | + sys.exit(1) |
| 71 | + # [END invite_user_with_access_role] |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 75 | + # home directory if none is specified. |
| 76 | + google_ads_client = GoogleAdsClient.load_from_storage() |
| 77 | + |
| 78 | + parser = argparse.ArgumentParser( |
| 79 | + description=( |
| 80 | + "Sends an invitation email to a user to manage a customer " |
| 81 | + "account with a desired access role." |
| 82 | + ) |
| 83 | + ) |
| 84 | + # The following argument(s) should be provided to run the example. |
| 85 | + parser.add_argument( |
| 86 | + "-c", |
| 87 | + "--customer_id", |
| 88 | + type=str, |
| 89 | + required=True, |
| 90 | + help="The Google Ads customer ID.", |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "-e", |
| 94 | + "--email_address", |
| 95 | + type=str, |
| 96 | + required=True, |
| 97 | + help="The email address of the user to send the invitation to.", |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "-a", |
| 101 | + "--access_role", |
| 102 | + type=str, |
| 103 | + required=True, |
| 104 | + choices=google_ads_client.get_type( |
| 105 | + "AccessRoleEnum", version="v6" |
| 106 | + ).AccessRole.keys(), |
| 107 | + help="The updated user access role.", |
| 108 | + ) |
| 109 | + args = parser.parse_args() |
| 110 | + |
| 111 | + main( |
| 112 | + google_ads_client, |
| 113 | + args.customer_id, |
| 114 | + args.email_address, |
| 115 | + args.access_role, |
| 116 | + ) |
0 commit comments