This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget_users.py
91 lines (74 loc) · 2.48 KB
/
get_users.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
80
81
82
83
84
85
86
87
88
89
90
91
"""
Gets list of users from Google Apps.
"""
import logging
import logging.handlers
import random
import sys
import time
import apiclient
import apiclient.discovery
import httplib2
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
logger = logging.getLogger('google-user-list')
logger.setLevel("INFO")
handler = logging.handlers.SysLogHandler(address='/dev/log')
formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def get_users(domain):
storage = Storage('get_users_credentials')
scopes = ('https://www.googleapis.com/auth/admin.directory.user.readonly')
credentials = storage.get()
if not credentials:
flow = flow_from_clientsecrets('client_secrets.json', scope=scopes, redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
print(auth_uri)
code = input("Auth token: ")
credentials = flow.step2_exchange(code)
storage.put(credentials)
http = httplib2.Http()
http = credentials.authorize(http)
service = apiclient.discovery.build("admin", 'directory_v1', http=http)
users = []
next_page_token = None
while True:
for retrycount in range(1, 4):
try:
users_page = service.users().list(
fields='nextPageToken,users(primaryEmail,suspended,suspensionReason)',
domain=domain,
pageToken=next_page_token,
maxResults=500
).execute()
next_page_token = users_page.get("nextPageToken")
users_page = users_page.get("users")
users.extend(users_page)
break
except IOError:
logger.warning("Downloading list of users failed")
time.sleep(retrycount)
if not next_page_token:
break
users_filtered = []
for user in users:
if not user.get("suspended", True):
users_filtered.append(user["primaryEmail"])
random.shuffle(users_filtered)
return users_filtered
def usage():
print("""
Usage: %s <domain>
Prints list of non-suspended users from Google Apps in random order
""")
def main():
if len(sys.argv) == 1:
usage()
sys.exit(1)
for domain in sys.argv[1:]:
users = get_users(domain)
for item in users:
print(item)
if __name__ == '__main__':
main()