Skip to content

Commit 9c927cc

Browse files
author
Shraddha
committed
Merging with default branch
2 parents 022ae6c + dcdfef6 commit 9c927cc

1 file changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/python
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+
"""Sample for the Provisioning API and the Email Settings API with OAuth 2.0"""
18+
19+
__author__ = 'Shraddha Gupta <shraddhag@google.com>'
20+
21+
from optparse import OptionParser
22+
import gdata.apps
23+
import gdata.apps.emailsettings.client
24+
import gdata.client
25+
import gdata.gauth
26+
27+
28+
API_VERSION = '2.0'
29+
BASE_URL = '/a/feeds/group/%s' % API_VERSION
30+
SCOPE = ('https://apps-apis.google.com/a/feeds/group '
31+
'https://apps-apis.google.com/a/feeds/emailsettings/2.0/')
32+
HOST = 'apps-apis.google.com'
33+
34+
35+
class OAuth2ClientSample(object):
36+
""" OAuth2ClientSample object demos the use of OAuth2Token for retrieving
37+
Members of a Group and updating Email Settings for them."""
38+
39+
def __init__(self, domain, client_id, client_secret):
40+
"""
41+
Args:
42+
domain: string Domain name (e.g. domain.com)
43+
client_id: string Client_id of domain admin account.
44+
client_secret: string Client_secret of domain admin account.
45+
"""
46+
try:
47+
self.token = gdata.gauth.OAuth2Token(client_id=client_id,
48+
client_secret=client_secret,
49+
scope=SCOPE,
50+
user_agent='oauth2-provisioningv2')
51+
self.uri = self.token.generate_authorize_url()
52+
print 'Please visit this URL to authorize the application:'
53+
print self.uri
54+
# Get the verification code from the standard input.
55+
code = raw_input('What is the verification code? ').strip()
56+
self.token.get_access_token(code)
57+
except gdata.gauth.OAuth2AccessTokenError, e:
58+
print 'Invalid Access token, Check your credentials %s' % e
59+
exit(0)
60+
self.domain = domain
61+
self.baseuri = '%s/%s' % (BASE_URL, domain)
62+
self.client = gdata.client.GDClient(host=HOST)
63+
self.client.domain = self.domain
64+
# Authorize the client.
65+
# This will add the Authorization header to all future requests.
66+
self.token.authorize(self.client)
67+
self.email_client = gdata.apps.emailsettings.client.EmailSettingsClient(
68+
domain=self.domain, auth_token=self.token)
69+
self.token.authorize(self.email_client)
70+
71+
def get_users(self, group):
72+
"""Retrieves members from the given group.
73+
74+
Args:
75+
group: string Id of the group
76+
77+
Returns:
78+
Member feed for the given group
79+
"""
80+
uri = '%s/%s/member' % (self.baseuri, group)
81+
try:
82+
feed = self.client.GetFeed(uri=uri)
83+
return gdata.apps.PropertyFeedFromString(str(feed))
84+
except gdata.client.RequestError, e:
85+
print 'Exception %s' % e
86+
87+
def create_filter(self, feed):
88+
"""Creates a mail filter that marks as read all messages not containing
89+
Domain name as one of their words for each member of the group.
90+
91+
Args:
92+
feed: PropertyFeed Member feed whose emailsettings need to be updated
93+
"""
94+
for entry in feed.entry:
95+
memberType = None
96+
memberId = None
97+
domain = None
98+
for i, property in enumerate(entry.property):
99+
if property.name == 'memberType':
100+
memberType = property.value
101+
if property.name == 'memberId':
102+
user_name, domain = property.value.split('@', 1)
103+
memberId = property.value
104+
# Check that the member is a User belonging to the primary Domain.
105+
if memberType == 'User' and domain == self.domain:
106+
print 'creating filter for %s' % memberId
107+
self.email_client.CreateFilter(user_name,
108+
does_not_have_the_word=self.domain,
109+
mark_as_read=True)
110+
elif memberType == 'User':
111+
print 'User belongs to other Domain %s' %memberId
112+
else:
113+
print 'Member is a group %s' %memberId
114+
115+
def run(self, group):
116+
feed = self.get_users(group)
117+
self.create_filter(feed)
118+
119+
120+
def main():
121+
"""Demos the Provisioning API and the Email Settings API with OAuth 2.0."""
122+
usage = 'usage: %prog [options]'
123+
parser = OptionParser(usage=usage)
124+
parser.add_option('--DOMAIN',
125+
help='Google Apps Domain, e.g. "domain.com".')
126+
parser.add_option('--CLIENT_ID',
127+
help='Registered CLIENT_ID of Domain.')
128+
parser.add_option('--CLIENT_SECRET',
129+
help='Registered CLIENT_SECRET of Domain.')
130+
parser.add_option('--GROUP',
131+
help='Group identifier')
132+
(options, args) = parser.parse_args()
133+
134+
if None in (options.DOMAIN, options.CLIENT_ID, options.CLIENT_SECRET,
135+
options.GROUP):
136+
parser.print_help()
137+
return
138+
139+
sample = OAuth2ClientSample(options.DOMAIN,
140+
options.CLIENT_ID, options.CLIENT_SECRET)
141+
sample.run(options.GROUP)
142+
143+
144+
if __name__ == '__main__':
145+
main()

0 commit comments

Comments
 (0)