|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""This example lists the resource names for the customers that the |
| 15 | +authenticating user has access to. |
| 16 | +
|
| 17 | +The customer IDs retrieved from the resource names can be used to set |
| 18 | +the login-customer-id configuration. For more information see this |
| 19 | +documentation: https://developers.google.com/google-ads/api/docs/concepts/call-structure#login-customer-id |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import absolute_import |
| 23 | + |
| 24 | +import sys |
| 25 | + |
| 26 | +import google.ads.google_ads.client |
| 27 | + |
| 28 | + |
| 29 | +def main(client): |
| 30 | + customer_service = client.get_service('CustomerService') |
| 31 | + |
| 32 | + try: |
| 33 | + accessible_customers = customer_service.list_accessible_customers() |
| 34 | + result_total = len(accessible_customers.resource_names) |
| 35 | + print('Total results: %i' % result_total) |
| 36 | + |
| 37 | + resource_names = accessible_customers.resource_names |
| 38 | + for resource_name in resource_names: |
| 39 | + print('Customer resource name: "%s"' % resource_name) |
| 40 | + except google.ads.google_ads.errors.GoogleAdsException as ex: |
| 41 | + print('Request with ID "%s" failed with status "%s" and includes the ' |
| 42 | + 'following errors:' % (ex.request_id, ex.error.code().name)) |
| 43 | + for error in ex.failure.errors: |
| 44 | + print('\tError with message "%s".' % error.message) |
| 45 | + if error.location: |
| 46 | + for field_path_element in error.location.field_path_elements: |
| 47 | + print('\t\tOn field: %s' % field_path_element.field_name) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 53 | + # home directory if none is specified. |
| 54 | + google_ads_client = (google.ads.google_ads.client.GoogleAdsClient |
| 55 | + .load_from_storage()) |
| 56 | + |
| 57 | + main(google_ads_client) |
0 commit comments