Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 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 | """Sample for the Group Settings API demonstrates get and update method. |
| 18 | |
| 19 | Usage: |
| 20 | $ python groupsettings.py |
| 21 | |
| 22 | You can also get help on all the command-line flags the program understands |
| 23 | by running: |
| 24 | |
| 25 | $ python groupsettings.py --help |
| 26 | """ |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 27 | from __future__ import print_function |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 28 | |
| 29 | __author__ = 'Shraddha Gupta <shraddhag@google.com>' |
| 30 | |
| 31 | from optparse import OptionParser |
| 32 | import os |
| 33 | |
| 34 | import pprint |
| 35 | import sys |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 36 | from googleapiclient.discovery import build |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 37 | import httplib2 |
| 38 | from oauth2client.client import flow_from_clientsecrets |
| 39 | from oauth2client.file import Storage |
Chaz Reid | e8e6c73 | 2018-07-14 06:45:07 -0700 | [diff] [blame] | 40 | from oauth2client.tools import run_flow |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 41 | |
| 42 | |
| 43 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 44 | # application, including client_id and client_secret, which are found |
| 45 | # on the API Access tab on the Google APIs |
| 46 | # Console <http://code.google.com/apis/console> |
| 47 | CLIENT_SECRETS = 'client_secrets.json' |
| 48 | |
| 49 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 50 | # is missing. |
| 51 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 52 | WARNING: Please configure OAuth 2.0 |
| 53 | |
| 54 | To make this sample run you will need to populate the client_secrets.json file |
| 55 | found at: |
| 56 | |
| 57 | %s |
| 58 | |
| 59 | with information from the APIs Console <https://code.google.com/apis/console>. |
| 60 | |
| 61 | """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) |
| 62 | |
| 63 | |
| 64 | def access_settings(service, groupId, settings): |
| 65 | """Retrieves a group's settings and updates the access permissions to it. |
| 66 | |
| 67 | Args: |
| 68 | service: object service for the Group Settings API. |
| 69 | groupId: string identifier of the group@domain. |
| 70 | settings: dictionary key-value pairs of properties of group. |
| 71 | """ |
| 72 | |
| 73 | # Get the resource 'group' from the set of resources of the API. |
| 74 | # The Group Settings API has only one resource 'group'. |
| 75 | group = service.groups() |
| 76 | |
| 77 | # Retrieve the group properties |
| 78 | g = group.get(groupUniqueId=groupId).execute() |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 79 | print('\nGroup properties for group %s\n' % g['name']) |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 80 | pprint.pprint(g) |
| 81 | |
| 82 | # If dictionary is empty, return without updating the properties. |
| 83 | if not settings.keys(): |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 84 | print('\nGive access parameters to update group access permissions\n') |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 85 | return |
| 86 | |
| 87 | body = {} |
| 88 | |
| 89 | # Settings might contain null value for some keys(properties). |
| 90 | # Extract the properties with values and add to dictionary body. |
| 91 | for key in settings.iterkeys(): |
| 92 | if settings[key] is not None: |
| 93 | body[key] = settings[key] |
| 94 | |
| 95 | # Update the properties of group |
| 96 | g1 = group.update(groupUniqueId=groupId, body=body).execute() |
| 97 | |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 98 | print('\nUpdated Access Permissions to the group\n') |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 99 | pprint.pprint(g1) |
| 100 | |
| 101 | |
| 102 | def main(argv): |
| 103 | """Demos the setting of the access properties by the Groups Settings API.""" |
| 104 | usage = 'usage: %prog [options]' |
| 105 | parser = OptionParser(usage=usage) |
| 106 | parser.add_option('--groupId', |
| 107 | help='Group email address') |
| 108 | parser.add_option('--whoCanInvite', |
| 109 | help='Possible values: ALL_MANAGERS_CAN_INVITE, ' |
| 110 | 'ALL_MEMBERS_CAN_INVITE') |
| 111 | parser.add_option('--whoCanJoin', |
| 112 | help='Possible values: ALL_IN_DOMAIN_CAN_JOIN, ' |
| 113 | 'ANYONE_CAN_JOIN, CAN_REQUEST_TO_JOIN, ' |
| 114 | 'CAN_REQUEST_TO_JOIN') |
| 115 | parser.add_option('--whoCanPostMessage', |
| 116 | help='Possible values: ALL_IN_DOMAIN_CAN_POST, ' |
| 117 | 'ALL_MANAGERS_CAN_POST, ALL_MEMBERS_CAN_POST, ' |
| 118 | 'ANYONE_CAN_POST, NONE_CAN_POST') |
| 119 | parser.add_option('--whoCanViewGroup', |
| 120 | help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, ' |
| 121 | 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, ' |
| 122 | 'ANYONE_CAN_VIEW') |
| 123 | parser.add_option('--whoCanViewMembership', |
| 124 | help='Possible values: ALL_IN_DOMAIN_CAN_VIEW, ' |
| 125 | 'ALL_MANAGERS_CAN_VIEW, ALL_MEMBERS_CAN_VIEW, ' |
| 126 | 'ANYONE_CAN_VIEW') |
| 127 | (options, args) = parser.parse_args() |
| 128 | |
| 129 | if options.groupId is None: |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 130 | print('Give the groupId for the group') |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 131 | parser.print_help() |
| 132 | return |
| 133 | |
| 134 | settings = {} |
| 135 | |
| 136 | if (options.whoCanInvite or options.whoCanJoin or options.whoCanPostMessage |
| 137 | or options.whoCanPostMessage or options.whoCanViewMembership) is None: |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 138 | print('No access parameters given in input to update access permissions') |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 139 | parser.print_help() |
| 140 | else: |
| 141 | settings = {'whoCanInvite': options.whoCanInvite, |
| 142 | 'whoCanJoin': options.whoCanJoin, |
| 143 | 'whoCanPostMessage': options.whoCanPostMessage, |
| 144 | 'whoCanViewGroup': options.whoCanViewGroup, |
| 145 | 'whoCanViewMembership': options.whoCanViewMembership} |
| 146 | |
| 147 | # Set up a Flow object to be used if we need to authenticate. |
| 148 | FLOW = flow_from_clientsecrets(CLIENT_SECRETS, |
| 149 | scope='https://www.googleapis.com/auth/apps.groups.settings', |
| 150 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 151 | |
| 152 | storage = Storage('groupsettings.dat') |
| 153 | credentials = storage.get() |
| 154 | |
| 155 | if credentials is None or credentials.invalid: |
INADA Naoki | e8d8782 | 2014-08-20 15:25:24 +0900 | [diff] [blame] | 156 | print('invalid credentials') |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 157 | # Save the credentials in storage to be used in subsequent runs. |
Chaz Reid | e8e6c73 | 2018-07-14 06:45:07 -0700 | [diff] [blame] | 158 | credentials = run_flow(FLOW, storage) |
Joe Gregorio | 53806d0 | 2011-11-23 09:08:31 -0500 | [diff] [blame] | 159 | |
| 160 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 161 | # with our good Credentials. |
| 162 | http = httplib2.Http() |
| 163 | http = credentials.authorize(http) |
| 164 | |
| 165 | service = build('groupssettings', 'v1', http=http) |
| 166 | |
| 167 | access_settings(service=service, groupId=options.groupId, settings=settings) |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | main(sys.argv) |