Skip to content

Commit 1f767b2

Browse files
author
Eric Koleda
committed
Add a main method to more quickstarts.
1 parent fa36ca4 commit 1f767b2

15 files changed

Lines changed: 348 additions & 239 deletions

File tree

admin_sdk/directory/quickstart.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
# [START admin_sdk_directory_quickstart]
1616
"""
17-
Shows basic usage of the Admin SDK Directory API. Lists of first 10 users in
17+
Shows basic usage of the Admin SDK Directory API. Lists the first 10 users in
1818
the domain.
1919
"""
2020
from __future__ import print_function
@@ -27,9 +27,7 @@
2727

2828

2929
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.
30+
"""Runs the sample.
3331
"""
3432
store = file.Storage('token.json')
3533
creds = store.get()

admin_sdk/reports/quickstart.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828

2929
def main():
30-
"""Shows basic usage of the Google Admin SDK Reports API.
31-
32-
Outputs a list of last 10 login events.
30+
"""Runs the sample.
3331
"""
3432
store = file.Storage('token.json')
3533
creds = store.get()

admin_sdk/reseller/quickstart.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828

2929
def main():
30-
"""Shows basic usage of the Google Admin-SDK Reseller API.
31-
32-
Outputs the first 10 subscriptions you manage.
30+
"""Runs the sample.
3331
"""
3432
store = file.Storage('token.json')
3533
creds = store.get()

apps_script/execute/execute.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414

1515
# [START apps_script_execute]
1616
def main():
17-
"""Shows basic usage of the Apps Script API.
18-
19-
Creates a Apps Script API service object and uses it to call an
20-
Apps Script function to print out a list of folders in the user's root
21-
directory.
17+
"""Runs the sample.
2218
"""
2319
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
2420

@@ -68,6 +64,7 @@ def main():
6864
# The API encountered a problem before the script started executing.
6965
print(e.content)
7066

67+
7168
if __name__ == '__main__':
7269
main()
7370
# [END apps_script_execute]

apps_script/quickstart/quickstart.py

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,60 @@
2424
from httplib2 import Http
2525
from oauth2client import file, client, tools
2626

27-
# Setup the Apps Script API
27+
# If modifying these scopes, delete the file token.json.
2828
SCOPES = 'https://www.googleapis.com/auth/script.projects'
29-
store = file.Storage('token.json')
30-
creds = store.get()
31-
if not creds or creds.invalid:
32-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
33-
creds = tools.run_flow(flow, store)
34-
service = build('script', 'v1', http=creds.authorize(Http()))
35-
36-
# Call the Apps Script API
37-
try:
38-
# Create a new project
39-
request = {'title2': 'My Script'}
40-
response = service.projects().create(body=request).execute()
41-
42-
# Upload two files to the project
43-
request = {
44-
'files': [{
45-
'name': 'hello',
46-
'type': 'SERVER_JS',
47-
'source': 'function helloWorld() {\n console.log("Hello, world!");\n}'
48-
}, {
49-
'name': 'appsscript',
50-
'type': 'JSON',
51-
'source': '{\"timeZone\":\"America/New_York\",\"exceptionLogging\":' + \
52-
'\"CLOUD\"}'
53-
}]
54-
}
55-
response = service.projects().updateContent(body=request,
56-
scriptId=response['scriptId']).execute()
57-
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
58-
except errors.HttpError as e:
59-
# The API encountered a problem.
60-
print(e.content)
29+
30+
SAMPLE_CODE = '''
31+
function helloWorld() {
32+
console.log("Hello, world!");
33+
}
34+
'''.strip()
35+
36+
SAMPLE_MANIFEST = '''
37+
{
38+
"timeZone": "America/New_York",
39+
"exceptionLogging": "CLOUD"
40+
}
41+
'''.strip()
42+
43+
44+
def main():
45+
"""Runs the sample.
46+
"""
47+
store = file.Storage('token.json')
48+
creds = store.get()
49+
if not creds or creds.invalid:
50+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
51+
creds = tools.run_flow(flow, store)
52+
service = build('script', 'v1', http=creds.authorize(Http()))
53+
54+
# Call the Apps Script API
55+
try:
56+
# Create a new project
57+
request = {'title': 'My Script'}
58+
response = service.projects().create(body=request).execute()
59+
60+
# Upload two files to the project
61+
request = {
62+
'files': [{
63+
'name': 'hello',
64+
'type': 'SERVER_JS',
65+
'source': SAMPLE_CODE
66+
}, {
67+
'name': 'appsscript',
68+
'type': 'JSON',
69+
'source': SAMPLE_MANIFEST
70+
}]
71+
}
72+
response = service.projects().updateContent(
73+
body=request,
74+
scriptId=response['scriptId']).execute()
75+
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
76+
except errors.HttpError as error:
77+
# The API encountered a problem.
78+
print(error.content)
79+
80+
81+
if __name__ == '__main__':
82+
main()
6183
# [END apps_script_quickstart]

calendar/quickstart/quickstart.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,35 @@
2323
from oauth2client import file, client, tools
2424
import datetime
2525

26-
# Setup the Calendar API
26+
# If modifying these scopes, delete the file token.json.
2727
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
28-
store = file.Storage('token.json')
29-
creds = store.get()
30-
if not creds or creds.invalid:
31-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
32-
creds = tools.run_flow(flow, store)
33-
service = build('calendar', 'v3', http=creds.authorize(Http()))
34-
35-
# Call the Calendar API
36-
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
37-
print('Getting the upcoming 10 events')
38-
events_result = service.events().list(calendarId='primary', timeMin=now,
39-
maxResults=10, singleEvents=True,
40-
orderBy='startTime').execute()
41-
events = events_result.get('items', [])
42-
43-
if not events:
44-
print('No upcoming events found.')
45-
for event in events:
46-
start = event['start'].get('dateTime', event['start'].get('date'))
47-
print(start, event['summary'])
28+
29+
30+
def main():
31+
"""Runs the sample.
32+
"""
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('calendar', 'v3', http=creds.authorize(Http()))
39+
40+
# Call the Calendar API
41+
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
42+
print('Getting the upcoming 10 events')
43+
events_result = service.events().list(calendarId='primary', timeMin=now,
44+
maxResults=10, singleEvents=True,
45+
orderBy='startTime').execute()
46+
events = events_result.get('items', [])
47+
48+
if not events:
49+
print('No upcoming events found.')
50+
for event in events:
51+
start = event['start'].get('dateTime', event['start'].get('date'))
52+
print(start, event['summary'])
53+
54+
55+
if __name__ == '__main__':
56+
main()
4857
# [END calendar_quickstart]

classroom/quickstart/quickstart.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@
2424
from httplib2 import Http
2525
from oauth2client import file, client, tools
2626

27-
# Setup the Classroom API
27+
# If modifying these scopes, delete the file token.json.
2828
SCOPES = 'https://www.googleapis.com/auth/classroom.courses.readonly'
29-
store = file.Storage('token.json')
30-
creds = store.get()
31-
if not creds or creds.invalid:
32-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
33-
creds = tools.run_flow(flow, store)
34-
service = build('classroom', 'v1', http=creds.authorize(Http()))
35-
36-
# Call the Classroom API
37-
results = service.courses().list(pageSize=10).execute()
38-
courses = results.get('courses', [])
39-
40-
if not courses:
41-
print('No courses found.')
42-
else:
43-
print('Courses:')
44-
for course in courses:
45-
print(course['name'])
29+
30+
31+
def main():
32+
"""Runs the sample.
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('classroom', 'v1', http=creds.authorize(Http()))
40+
41+
# Call the Classroom API
42+
results = service.courses().list(pageSize=10).execute()
43+
courses = results.get('courses', [])
44+
45+
if not courses:
46+
print('No courses found.')
47+
else:
48+
print('Courses:')
49+
for course in courses:
50+
print(course['name'])
51+
52+
53+
if __name__ == '__main__':
54+
main()
4655
# [END classroom_quickstart]

drive/activity/quickstart.py

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,44 @@
1919
from oauth2client import file, client, tools
2020
import datetime
2121

22-
# Setup the Drive Activity API
23-
SCOPES = 'https://www.googleapis.com/auth/activity https://www.googleapis.com/auth/drive.metadata.readonly'
24-
store = file.Storage('token.json')
25-
creds = store.get()
26-
if not creds or creds.invalid:
27-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
28-
creds = tools.run_flow(flow, store)
29-
service = build('appsactivity', 'v1', http=creds.authorize(Http()))
22+
# If modifying these scopes, delete the file token.json.
23+
SCOPES = 'https://www.googleapis.com/auth/activity'
3024

31-
# Call the Drive Activity API
32-
results = service.activities().list(source='drive.google.com',
33-
drive_ancestorId='root', pageSize=10).execute()
34-
activities = results.get('activities', [])
35-
if not activities:
36-
print('No activity.')
37-
else:
38-
print('Recent activity:')
39-
for activity in activities:
40-
event = activity['combinedEvent']
41-
user = event.get('user', None)
42-
target = event.get('target', None)
43-
if user == None or target == None:
44-
continue
45-
time = datetime.datetime.fromtimestamp(
46-
int(event['eventTimeMillis'])/1000)
47-
print('{0}: {1}, {2}, {3} ({4})'.format(time, user['name'],
48-
event['primaryEventType'], target['name'], target['mimeType']))
25+
26+
def main():
27+
"""Runs the sample.
28+
"""
29+
store = file.Storage('token.json')
30+
creds = store.get()
31+
if not creds or creds.invalid:
32+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
33+
creds = tools.run_flow(flow, store)
34+
service = build('appsactivity', 'v1', http=creds.authorize(Http()))
35+
36+
# Call the Drive Activity API
37+
results = service.activities().list(source='drive.google.com',
38+
drive_ancestorId='root',
39+
pageSize=10).execute()
40+
activities = results.get('activities', [])
41+
if not activities:
42+
print('No activity.')
43+
else:
44+
print('Recent activity:')
45+
for activity in activities:
46+
event = activity['combinedEvent']
47+
user = event.get('user', None)
48+
target = event.get('target', None)
49+
if user is None or target is None:
50+
continue
51+
time = datetime.datetime.fromtimestamp(
52+
int(event['eventTimeMillis'])/1000)
53+
print(u'{0}: {1}, {2}, {3} ({4})'.format(time,
54+
user['name'],
55+
event['primaryEventType'],
56+
target['name'],
57+
target['mimeType']))
58+
59+
60+
if __name__ == '__main__':
61+
main()
4962
# [END drive_activity_quickstart]

drive/quickstart/quickstart.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@
2424
from httplib2 import Http
2525
from oauth2client import file, client, tools
2626

27-
# Setup the Drive v3 API
27+
# If modifying these scopes, delete the file token.json.
2828
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
29-
store = file.Storage('token.json')
30-
creds = store.get()
31-
if not creds or creds.invalid:
32-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
33-
creds = tools.run_flow(flow, store)
34-
service = build('drive', 'v3', http=creds.authorize(Http()))
35-
36-
# Call the Drive v3 API
37-
results = service.files().list(
38-
pageSize=10, fields="nextPageToken, files(id, name)").execute()
39-
items = results.get('files', [])
40-
if not items:
41-
print('No files found.')
42-
else:
43-
print('Files:')
44-
for item in items:
45-
print('{0} ({1})'.format(item['name'], item['id']))
29+
30+
31+
def main():
32+
"""Runs the sample.
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('drive', 'v3', http=creds.authorize(Http()))
40+
41+
# Call the Drive v3 API
42+
results = service.files().list(
43+
pageSize=10, fields="nextPageToken, files(id, name)").execute()
44+
items = results.get('files', [])
45+
if not items:
46+
print('No files found.')
47+
else:
48+
print('Files:')
49+
for item in items:
50+
print(u'{0} ({1})'.format(item['name'], item['id']))
51+
52+
53+
if __name__ == '__main__':
54+
main()
4655
# [END drive_quickstart]

0 commit comments

Comments
 (0)