|
| 1 | +#!/usr/bin/python2.4 |
| 2 | +# |
| 3 | +# Copyright 2011 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 | +"""Demonstrates Email Settings API's POP settings. |
| 18 | + Enables POP for all of the domain's users for all messages and |
| 19 | + archives Gmail's copy. |
| 20 | +""" |
| 21 | + |
| 22 | +__author__ = 'Gunjan Sharma <gunjansharma@google.com>' |
| 23 | + |
| 24 | +import getopt |
| 25 | +import sys |
| 26 | +import gdata.apps.client |
| 27 | +import gdata.apps.emailsettings.client |
| 28 | + |
| 29 | + |
| 30 | +SCOPES = ['https://apps-apis.google.com/a/feeds/emailsettings/2.0/', |
| 31 | + 'https://apps-apis.google.com/a/feeds/user/'] |
| 32 | + |
| 33 | + |
| 34 | +class PopSettingsException(Exception): |
| 35 | + """Exception class for PopSettings to show appropriate error message.""" |
| 36 | + |
| 37 | + def __init__(self, message): |
| 38 | + """Create a new PopSettingsException with the appropriate error message.""" |
| 39 | + super(PopSettingsException, self).__init__(message) |
| 40 | + |
| 41 | + |
| 42 | +class PopSettings(object): |
| 43 | + """Sample demonstrating how to enable POP for all of a domain's users.""" |
| 44 | + |
| 45 | + def __init__(self, consumer_key, consumer_secret, domain): |
| 46 | + """Create a new PopSettings object configured for a domain. |
| 47 | +
|
| 48 | + Args: |
| 49 | + consumer_key: [string] The consumerKey of the domain. |
| 50 | + consumer_secret: [string] The consumerSecret of the domain. |
| 51 | + domain: [string] The domain whose user's POP settings to be changed. |
| 52 | + """ |
| 53 | + self.consumer_key = consumer_key |
| 54 | + self.consumer_secret = consumer_secret |
| 55 | + self.domain = domain |
| 56 | + |
| 57 | + def Authorize(self): |
| 58 | + """Asks the domain's admin to authorize. |
| 59 | +
|
| 60 | + Access to two APIs needs to be authorized, |
| 61 | + provisioning users and Gmail settings. |
| 62 | + The function creates clients for both APIs. |
| 63 | + """ |
| 64 | + self.email_settings_client = ( |
| 65 | + gdata.apps.emailsettings.client.EmailSettingsClient( |
| 66 | + domain=self.domain)) |
| 67 | + self.provisioning_client = gdata.apps.client.AppsClient(domain=self.domain) |
| 68 | + request_token = self.email_settings_client.GetOAuthToken( |
| 69 | + SCOPES, None, self.consumer_key, consumer_secret=self.consumer_secret) |
| 70 | + print request_token.GenerateAuthorizationUrl() |
| 71 | + raw_input('Manually go to the above URL and authenticate.' |
| 72 | + 'Press Return after authorization.') |
| 73 | + access_token = self.email_settings_client.GetAccessToken(request_token) |
| 74 | + self.email_settings_client.auth_token = access_token |
| 75 | + self.provisioning_client.auth_token = access_token |
| 76 | + |
| 77 | + def UpdateDomainUsersPopSettings(self): |
| 78 | + """Updates POP settings for all of the domain's users.""" |
| 79 | + users = self.provisioning_client.RetrieveAllUsers() |
| 80 | + for user in users.entry: |
| 81 | + self.email_settings_client.UpdatePop(username=user.login.user_name, |
| 82 | + enable=True, |
| 83 | + enable_for='ALL_MAIL', |
| 84 | + action='ARCHIVE') |
| 85 | + |
| 86 | + |
| 87 | +def PrintUsageString(): |
| 88 | + """Prints the correct call for running the sample.""" |
| 89 | + print ('python emailsettings_pop_settings.py' |
| 90 | + '--consumer_key [ConsumerKey] --consumer_secret [ConsumerSecret]' |
| 91 | + '--domain [domain]') |
| 92 | + |
| 93 | + |
| 94 | +def main(): |
| 95 | + """Updates POP settings for all of the domain's users |
| 96 | + using the Email Settings API. |
| 97 | + """ |
| 98 | + try: |
| 99 | + opts, args = getopt.getopt(sys.argv[1:], '', ['consumer_key=', |
| 100 | + 'consumer_secret=', |
| 101 | + 'domain=']) |
| 102 | + except getopt.error, msg: |
| 103 | + PrintUsageString() |
| 104 | + sys.exit(1) |
| 105 | + |
| 106 | + consumer_key = '' |
| 107 | + consumer_secret = '' |
| 108 | + domain = '' |
| 109 | + for option, arg in opts: |
| 110 | + if option == '--consumer_key': |
| 111 | + consumer_key = arg |
| 112 | + elif option == '--consumer_secret': |
| 113 | + consumer_secret = arg |
| 114 | + elif option == '--domain': |
| 115 | + domain = arg |
| 116 | + |
| 117 | + if not (consumer_key and consumer_secret and domain): |
| 118 | + print 'Requires exactly three flags.' |
| 119 | + PrintUsageString() |
| 120 | + sys.exit(1) |
| 121 | + |
| 122 | + pop_settings = PopSettings( |
| 123 | + consumer_key, consumer_secret, domain) |
| 124 | + try: |
| 125 | + pop_settings.Authorize() |
| 126 | + pop_settings.UpdateDomainUsersPopSettings() |
| 127 | + except gdata.client.RequestError, e: |
| 128 | + if e.status == 403: |
| 129 | + raise PopSettingsException('Invalid Domain') |
| 130 | + elif e.status == 400: |
| 131 | + raise PopSettingsException('Invalid consumer credentials') |
| 132 | + elif e.status == 503: |
| 133 | + raise PopSettingsException('Server busy') |
| 134 | + else e.status == 500: |
| 135 | + raise PopSettingsException('Internal server error') |
| 136 | + else: |
| 137 | + raise PopSettingsException('Unknown error') |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == '__main__': |
| 141 | + main() |
0 commit comments