|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2021 Google LLC All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Analytics Admin API sample application which creates a access binding for |
| 18 | +the account using a batch call. |
| 19 | +
|
| 20 | +See https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/accounts.accessBindings/batchCreate |
| 21 | +for more information. |
| 22 | +""" |
| 23 | +# [START analyticsadmin_accounts_access_bindings_batch_create] |
| 24 | +from google.analytics.admin import AnalyticsAdminServiceClient |
| 25 | +from google.analytics.admin_v1alpha.types import ( |
| 26 | + AccessBinding, |
| 27 | + BatchCreateAccessBindingsRequest, |
| 28 | + CreateAccessBindingRequest, |
| 29 | +) |
| 30 | + |
| 31 | + |
| 32 | +def run_sample(): |
| 33 | + """Runs the sample.""" |
| 34 | + |
| 35 | + # !!! ATTENTION !!! |
| 36 | + # Running this sample may change/delete your Google Analytics account |
| 37 | + # configuration. Make sure to not use the Google Analytics account ID from |
| 38 | + # your production environment below. |
| 39 | + |
| 40 | + # TODO(developer): Replace this variable with your Google Analytics |
| 41 | + # account ID (e.g. "123456") before running the sample. |
| 42 | + account_id = "YOUR-GA-ACCOUNT-ID" |
| 43 | + |
| 44 | + # TODO(developer): Replace this variable with an email address of the user to |
| 45 | + # link. This user will be given access to your account after running the |
| 46 | + # sample. |
| 47 | + email_address = "TEST-EMAIL-ADDRESS" |
| 48 | + |
| 49 | + batch_create_account_access_binding(account_id, email_address) |
| 50 | + |
| 51 | + |
| 52 | +def batch_create_account_access_binding( |
| 53 | + account_id: str, email_address: str, transport: str = None |
| 54 | +): |
| 55 | + """ |
| 56 | + Creates a access binding for the account using a batch call. |
| 57 | +
|
| 58 | + Args: |
| 59 | + account_id(str): The Google Analytics Account ID. |
| 60 | + email_address(str): Email address of the access binding user. |
| 61 | + transport(str): The transport to use. For example, "grpc" |
| 62 | + or "rest". If set to None, a transport is chosen automatically. |
| 63 | + """ |
| 64 | + client = AnalyticsAdminServiceClient(transport=transport) |
| 65 | + response = client.batch_create_access_bindings( |
| 66 | + BatchCreateAccessBindingsRequest( |
| 67 | + parent=f"accounts/{account_id}", |
| 68 | + requests=[ |
| 69 | + CreateAccessBindingRequest( |
| 70 | + access_binding=AccessBinding( |
| 71 | + user=email_address, |
| 72 | + roles=["predefinedRoles/read"], |
| 73 | + ) |
| 74 | + ) |
| 75 | + ], |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + print("Result:") |
| 80 | + for access_binding in response.access_bindings: |
| 81 | + print(access_binding) |
| 82 | + print() |
| 83 | + |
| 84 | + |
| 85 | +# [END analyticsadmin_accounts_access_bindings_batch_create] |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + run_sample() |
0 commit comments