1#!/usr/bin/python 2# 3# Copyright 2014 Google Inc. 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"""Sample for the Group Settings API demonstrates get and update method. 18 19Usage: 20 $ python groupsettings.py 21 22You can also get help on all the command-line flags the program understands 23by running: 24 25 $ python groupsettings.py --help 26""" 27from __future__ import print_function 28 29__author__ = 'Shraddha Gupta <shraddhag@google.com>' 30 31from optparse import OptionParser 32import os 33 34import pprint 35import sys 36from googleapiclient.discovery import build 37import httplib2 38from oauth2client.client import flow_from_clientsecrets 39from oauth2client.file import Storage 40from oauth2client.tools import run_flow 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> 47CLIENT_SECRETS = 'client_secrets.json' 48 49# Helpful message to display in the browser if the CLIENT_SECRETS file 50# is missing. 51MISSING_CLIENT_SECRETS_MESSAGE = """ 52WARNING: Please configure OAuth 2.0 53 54To make this sample run you will need to populate the client_secrets.json file 55found at: 56 57 %s 58 59with information from the APIs Console <https://code.google.com/apis/console>. 60 61""" % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) 62 63 64def 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() 79 print('\nGroup properties for group %s\n' % g['name']) 80 pprint.pprint(g) 81 82 # If dictionary is empty, return without updating the properties. 83 if not settings.keys(): 84 print('\nGive access parameters to update group access permissions\n') 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 98 print('\nUpdated Access Permissions to the group\n') 99 pprint.pprint(g1) 100 101 102def 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: 130 print('Give the groupId for the group') 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: 138 print('No access parameters given in input to update access permissions') 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: 156 print('invalid credentials') 157 # Save the credentials in storage to be used in subsequent runs. 158 credentials = run_flow(FLOW, storage) 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 169if __name__ == '__main__': 170 main(sys.argv) 171