-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_groups_rest.py
executable file
·79 lines (59 loc) · 3.08 KB
/
google_groups_rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
import httplib2
import json
from oauth2client.client import SignedJwtAssertionCredentials
from urllib import urlencode
# The module mydb should be implemented by you - provide your credentials
from mydb import get_path_to_p12, get_email, get_service_email
FULL_PATH_TO_P12 = get_path_to_p12()
ACCOUNT_EMAIL = get_email()
SERVICE_EMAIL = get_service_email()
def get_group_members(group):
url = 'https://www.googleapis.com/admin/directory/v1/groups/{}/members'.format(group)
return call_google_api("GET", url)
def remove_group_member(group, member):
url = 'https://www.googleapis.com/admin/directory/v1/groups/{}/members/{}'.format(group, member)
return call_google_api("DELETE", url)
def add_group_member(group, payload=False):
url = 'https://www.googleapis.com/admin/directory/v1/groups/{}/members'.format(group)
return call_google_api("POST", url, payload)
def call_google_api(method, url, payload=False):
content = {}
try:
http = get_conn()
if payload:
(resp, content) = http.request(uri=url, method=method, body=json.dumps(payload), headers={'Content-type':'application/json'})
else:
(resp, content) = http.request(uri=url, method=method, headers={'Content-type':'application/json'})
except Exception as e:
print "Failed to post request to [{}] due to: {}".format(url, e)
return json.loads(content)
def get_conn():
with open(FULL_PATH_TO_P12) as f:
private_key = f.read()
OAUTH_SCOPE = ['https://www.googleapis.com/auth/admin.directory.group.member',
'https://www.googleapis.com/auth/admin.directory.group']
credentials = SignedJwtAssertionCredentials(SERVICE_EMAIL, private_key, OAUTH_SCOPE, sub=ACCOUNT_EMAIL)
http = httplib2.Http()
return credentials.authorize(http)
# Unit test
if __name__ == '__main__':
payload = {
"email": "alfasin@your-organization.com'",
"role": "MEMBER",
}
print "\n ---------------------------------- \n"
print "calling get_group_members('google-group-example@your-organization.com')"
res = get_group_members('google-group-example@your-organization.com')
print json.dumps(res, indent=4, sort_keys=True)
print "calling add_group_member('alfasin@your-organization.com', 'google-group-example@your-organization.com')"
add_group_member('alfasin@your-organization.com', payload)
print "calling get_group_members('google-group-example@your-organization.com')"
res = get_group_members('google-group-example@your-organization.com')
print json.dumps(res, indent=4, sort_keys=True)
print "calling remove_group_member('google-group-example@your-organization.com', 'alfasin@your-organization.com')"
res = remove_group_member('google-group-example@your-organization.com', 'alfasin@your-organization.com')
print "calling get_group_members('google-group-example@your-organization.com')"
res = get_group_members('google-group-example@your-organization.com')
print json.dumps(res, indent=4, sort_keys=True)
print "\n ---------------------------------- \n"