Skip to content

Commit fa36ca4

Browse files
author
Eric Koleda
committed
Add a main function to the Admin SDK Quickstarts.
1 parent f9cf390 commit fa36ca4

File tree

3 files changed

+100
-76
lines changed

3 files changed

+100
-76
lines changed

admin_sdk/directory/quickstart.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,45 @@
1414

1515
# [START admin_sdk_directory_quickstart]
1616
"""
17-
Shows basic usage of the Admin SDK Directory API. Lists of first 10 users in the
18-
domain.
17+
Shows basic usage of the Admin SDK Directory API. Lists of first 10 users in
18+
the domain.
1919
"""
2020
from __future__ import print_function
2121
from apiclient.discovery import build
2222
from httplib2 import Http
2323
from oauth2client import file, client, tools
2424

25-
# Setup the Admin SDK Directory API
25+
# If modifying these scopes, delete the file token.json.
2626
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
27-
store = file.Storage('token.json')
28-
creds = store.get()
29-
if not creds or creds.invalid:
30-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
31-
creds = tools.run_flow(flow, store)
32-
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
33-
34-
# Call the Admin SDK Directory API
35-
print('Getting the first 10 users in the domain')
36-
results = service.users().list(customer='my_customer', maxResults=10,
37-
orderBy='email').execute()
38-
users = results.get('users', [])
39-
40-
if not users:
41-
print('No users in the domain.')
42-
else:
43-
print('Users:')
44-
for user in users:
45-
print('{0} ({1})'.format(user['primaryEmail'], user['name']['fullName']))
27+
28+
29+
def main():
30+
"""Shows basic usage of the Google Admin SDK Directory API.
31+
32+
Outputs a list of first 10 users in the domain.
33+
"""
34+
store = file.Storage('token.json')
35+
creds = store.get()
36+
if not creds or creds.invalid:
37+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
38+
creds = tools.run_flow(flow, store)
39+
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
40+
41+
# Call the Admin SDK Directory API
42+
print('Getting the first 10 users in the domain')
43+
results = service.users().list(customer='my_customer', maxResults=10,
44+
orderBy='email').execute()
45+
users = results.get('users', [])
46+
47+
if not users:
48+
print('No users in the domain.')
49+
else:
50+
print('Users:')
51+
for user in users:
52+
print(u'{0} ({1})'.format(user['primaryEmail'],
53+
user['name']['fullName']))
54+
55+
56+
if __name__ == '__main__':
57+
main()
4658
# [END admin_sdk_directory_quickstart]

admin_sdk/reports/quickstart.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,41 @@
1818
events.
1919
"""
2020
from __future__ import print_function
21-
from apiclient.discovery import build, http
21+
from apiclient.discovery import build
2222
from httplib2 import Http
2323
from oauth2client import file, client, tools
24-
import StringIO
25-
import random
2624

27-
import apiclient
28-
from email import Utils
29-
from email import MIMEText
30-
31-
# Setup the Admin SDK Reports API
25+
# If modifying these scopes, delete the file token.json.
3226
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
33-
store = file.Storage('token.json')
34-
creds = store.get()
35-
if not creds or creds.invalid:
36-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
37-
creds = tools.run_flow(flow, store)
38-
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
39-
40-
print('Getting the last 10 login events')
41-
results = service.activities().list(userKey='all', applicationName='login',
42-
maxResults=10).execute()
43-
activities = results.get('items', [])
44-
45-
if not activities:
46-
print('No logins found.')
47-
else:
48-
print('Logins:')
49-
for activity in activities:
50-
print('{0}: {1} ({2})'.format(activity['id']['time'],
51-
activity['actor']['email'], activity['events'][0]['name']))
27+
28+
29+
def main():
30+
"""Shows basic usage of the Google Admin SDK Reports API.
31+
32+
Outputs a list of last 10 login events.
33+
"""
34+
store = file.Storage('token.json')
35+
creds = store.get()
36+
if not creds or creds.invalid:
37+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
38+
creds = tools.run_flow(flow, store)
39+
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
40+
41+
print('Getting the last 10 login events')
42+
results = service.activities().list(userKey='all', applicationName='login',
43+
maxResults=10).execute()
44+
activities = results.get('items', [])
45+
46+
if not activities:
47+
print('No logins found.')
48+
else:
49+
print('Logins:')
50+
for activity in activities:
51+
print(u'{0}: {1} ({2})'.format(activity['id']['time'],
52+
activity['actor']['email'],
53+
activity['events'][0]['name']))
54+
55+
56+
if __name__ == '__main__':
57+
main()
5258
# [END admin_sdk_reports_quickstart]

admin_sdk/reseller/quickstart.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,41 @@
1818
events.
1919
"""
2020
from __future__ import print_function
21-
from apiclient.discovery import build, http
21+
from apiclient.discovery import build
2222
from httplib2 import Http
2323
from oauth2client import file, client, tools
24-
import StringIO
25-
import random
2624

27-
import apiclient
28-
from email import Utils
29-
from email import MIMEText
30-
31-
# Setup the Admin SDK Reports API
25+
# If modifying these scopes, delete the file token.json.
3226
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
33-
store = file.Storage('token.json')
34-
creds = store.get()
35-
if not creds or creds.invalid:
36-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
37-
creds = tools.run_flow(flow, store)
38-
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
39-
40-
print('Getting the last 10 login events')
41-
results = service.activities().list(userKey='all', applicationName='login',
42-
maxResults=10).execute()
43-
activities = results.get('items', [])
44-
45-
if not activities:
46-
print('No logins found.')
47-
else:
48-
print('Logins:')
49-
for activity in activities:
50-
print('{0}: {1} ({2})'.format(activity['id']['time'],
51-
activity['actor']['email'], activity['events'][0]['name']))
27+
28+
29+
def main():
30+
"""Shows basic usage of the Google Admin-SDK Reseller API.
31+
32+
Outputs the first 10 subscriptions you manage.
33+
"""
34+
store = file.Storage('token.json')
35+
creds = store.get()
36+
if not creds or creds.invalid:
37+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
38+
creds = tools.run_flow(flow, store)
39+
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
40+
41+
print('Getting the last 10 login events')
42+
results = service.activities().list(userKey='all', applicationName='login',
43+
maxResults=10).execute()
44+
activities = results.get('items', [])
45+
46+
if not activities:
47+
print('No logins found.')
48+
else:
49+
print('Logins:')
50+
for activity in activities:
51+
print(u'{0}: {1} ({2})'.format(activity['id']['time'],
52+
activity['actor']['email'],
53+
activity['events'][0]['name']))
54+
55+
56+
if __name__ == '__main__':
57+
main()
5258
# [END admin_sdk_reseller_quickstart]

0 commit comments

Comments
 (0)