Skip to content

Commit 9da7c44

Browse files
committed
Standardize python quickstart nits
2 parents dc33062 + e33a875 commit 9da7c44

File tree

15 files changed

+417
-334
lines changed

15 files changed

+417
-334
lines changed

admin_sdk/directory/quickstart.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,40 @@
1313
# limitations under the License.
1414

1515
# [START admin_sdk_directory_quickstart]
16-
"""
17-
Shows basic usage of the Admin SDK Directory API. Lists of first 10 users in the
18-
domain.
19-
"""
2016
from __future__ import print_function
2117
from googleapiclient.discovery import build
2218
from httplib2 import Http
2319
from oauth2client import file, client, tools
2420

25-
# Setup the Admin SDK Directory API
21+
# If modifying these scopes, delete the file token.json.
2622
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()))
3323

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', [])
24+
def main():
25+
"""Shows basic usage of the Admin SDK Directory API.
26+
Prints the emails and names of the first 10 users in the domain.
27+
"""
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('admin', 'directory_v1', http=creds.authorize(Http()))
3934

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'],
46-
user['name']['fullName']))
35+
# Call the Admin SDK Directory API
36+
print('Getting the first 10 users in the domain')
37+
results = service.users().list(customer='my_customer', maxResults=10,
38+
orderBy='email').execute()
39+
users = results.get('users', [])
40+
41+
if not users:
42+
print('No users in the domain.')
43+
else:
44+
print('Users:')
45+
for user in users:
46+
print('{0} ({1})'.format(user['primaryEmail'],
47+
user['name']['fullName']))
48+
49+
50+
if __name__ == '__main__':
51+
main()
4752
# [END admin_sdk_directory_quickstart]

admin_sdk/reports/quickstart.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,39 @@
1313
# limitations under the License.
1414

1515
# [START admin_sdk_reports_quickstart]
16-
"""
17-
Shows basic usage of the Admin SDK Reports API. Outputs a list of last 10 login
18-
events.
19-
"""
2016
from __future__ import print_function
2117
from googleapiclient.discovery import build
2218
from httplib2 import Http
2319
from oauth2client import file, client, tools
2420

25-
# Setup the Admin SDK Reports API
21+
# If modifying these scopes, delete the file token.json.
2622
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
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', 'reports_v1', http=creds.authorize(Http()))
3323

34-
print('Getting the last 10 login events')
35-
results = service.activities().list(userKey='all', applicationName='login',
36-
maxResults=10).execute()
37-
activities = results.get('items', [])
24+
def main():
25+
"""Shows basic usage of the Admin SDK Reports API.
26+
Prints the time, email, and name of the last 10 login events in the domain.
27+
"""
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('admin', 'reports_v1', http=creds.authorize(Http()))
3834

39-
if not activities:
40-
print('No logins found.')
41-
else:
42-
print('Logins:')
43-
for activity in activities:
44-
print('{0}: {1} ({2})'.format(activity['id']['time'],
45-
activity['actor']['email'], activity['events'][0]['name']))
35+
# Call the Admin SDK Reports API
36+
print('Getting the last 10 login events')
37+
results = service.activities().list(userKey='all', applicationName='login',
38+
maxResults=10).execute()
39+
activities = results.get('items', [])
40+
41+
if not activities:
42+
print('No logins found.')
43+
else:
44+
print('Logins:')
45+
for activity in activities:
46+
print('{0}: {1} ({2})'.format(activity['id']['time'],
47+
activity['actor']['email'], activity['events'][0]['name']))
48+
49+
if __name__ == '__main__':
50+
main()
4651
# [END admin_sdk_reports_quickstart]

admin_sdk/reseller/quickstart.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,39 @@
1313
# limitations under the License.
1414

1515
# [START admin_sdk_reseller_quickstart]
16-
"""
17-
Shows basic usage of the Admin SDK Reports API. Outputs a list of last 10 login
18-
events.
19-
"""
2016
from __future__ import print_function
2117
from googleapiclient.discovery import build
2218
from httplib2 import Http
2319
from oauth2client import file, client, tools
2420

25-
# Setup the Admin SDK Reports API
26-
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
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', 'reports_v1', http=creds.authorize(Http()))
21+
# If modifying these scopes, delete the file token.json.
22+
SCOPES = 'https://www.googleapis.com/auth/apps.order'
3323

34-
print('Getting the last 10 login events')
35-
results = service.activities().list(userKey='all', applicationName='login',
36-
maxResults=10).execute()
37-
activities = results.get('items', [])
24+
def main():
25+
"""Calls the Admin SDK Reseller API. Prints the customer ID, SKU ID,
26+
and plan name of the first 10 subscriptions managed by the domain.
27+
"""
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('admin', 'reports_v1', http=creds.authorize(Http()))
3834

39-
if not activities:
40-
print('No logins found.')
41-
else:
42-
print('Logins:')
43-
for activity in activities:
44-
print('{0}: {1} ({2})'.format(activity['id']['time'],
45-
activity['actor']['email'], activity['events'][0]['name']))
35+
# Call the Admin SDK Reseller API
36+
print('Getting the last 10 login events')
37+
results = service.activities().list(userKey='all', applicationName='login',
38+
maxResults=10).execute()
39+
activities = results.get('items', [])
40+
41+
if not activities:
42+
print('No logins found.')
43+
else:
44+
print('Logins:')
45+
for activity in activities:
46+
print('{0}: {1} ({2})'.format(activity['id']['time'],
47+
activity['actor']['email'], activity['events'][0]['name']))
48+
49+
if __name__ == '__main__':
50+
main()
4651
# [END admin_sdk_reseller_quickstart]

apps_script/execute/execute.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,21 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# [START apps_script_execute]
15+
# [START apps_script_api_execute]
1616
from __future__ import print_function
1717
from apiclient import errors
1818
from apiclient.discovery import build
1919
from httplib2 import Http
20-
from oauth2client import file as oauthfile, client, tools
20+
from oauth2client import file as oauth_file, client, tools
2121

2222
def main():
23-
"""Shows basic usage of the Apps Script API.
24-
25-
Creates a Apps Script API service object and uses it to call an
26-
Apps Script function to print out a list of folders in the user's root
27-
directory.
23+
"""Runs the sample.
2824
"""
2925
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
3026

3127
# Setup the Apps Script API
3228
SCOPES = 'https://www.googleapis.com/auth/script.projects'
33-
store = oauthfile.Storage('token.json')
29+
store = oauth_file.Storage('token.json')
3430
creds = store.get()
3531
if not creds or creds.invalid:
3632
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
@@ -78,6 +74,7 @@ def main():
7874
# The API encountered a problem before the script started executing.
7975
print(e.content)
8076

77+
8178
if __name__ == '__main__':
8279
main()
83-
# [END apps_script_execute]
80+
# [END apps_script_api_execute]

apps_script/quickstart/quickstart.py

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,59 @@
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 ' \
48-
'console.log("Hello, world!");\n}'
49-
}, {
50-
'name': 'appsscript',
51-
'type': 'JSON',
52-
'source': '{\"timeZone\":\"America/New_York\",' \
53-
'\"exceptionLogging\":\"CLOUD\"}'
54-
}]
55-
}
56-
response = service.projects().updateContent(body=request,
57-
scriptId=response['scriptId']).execute()
58-
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
59-
except errors.HttpError as e:
60-
# The API encountered a problem.
61-
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+
def main():
44+
"""Calls the Apps Script API.
45+
"""
46+
store = file.Storage('token.json')
47+
creds = store.get()
48+
if not creds or creds.invalid:
49+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
50+
creds = tools.run_flow(flow, store)
51+
service = build('script', 'v1', http=creds.authorize(Http()))
52+
53+
# Call the Apps Script API
54+
try:
55+
# Create a new project
56+
request = {'title': 'My Script'}
57+
response = service.projects().create(body=request).execute()
58+
59+
# Upload two files to the project
60+
request = {
61+
'files': [{
62+
'name': 'hello',
63+
'type': 'SERVER_JS',
64+
'source': SAMPLE_CODE
65+
}, {
66+
'name': 'appsscript',
67+
'type': 'JSON',
68+
'source': SAMPLE_MANIFEST
69+
}]
70+
}
71+
response = service.projects().updateContent(
72+
body=request,
73+
scriptId=response['scriptId']).execute()
74+
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
75+
except errors.HttpError as error:
76+
# The API encountered a problem.
77+
print(error.content)
78+
79+
80+
if __name__ == '__main__':
81+
main()
6282
# [END apps_script_quickstart]

calendar/quickstart/quickstart.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,40 @@
1313
# limitations under the License.
1414

1515
# [START calendar_quickstart]
16-
"""
17-
Shows basic usage of the Google Calendar API. Creates a Google Calendar API
18-
service object and outputs a list of the next 10 events on the user's calendar.
19-
"""
2016
from __future__ import print_function
2117
import datetime
2218
from googleapiclient.discovery import build
2319
from httplib2 import Http
2420
from oauth2client import file, client, tools
2521

26-
# Setup the Calendar API
22+
# If modifying these scopes, delete the file token.json.
2723
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()))
3424

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', [])
25+
def main():
26+
"""Shows basic usage of the Google Calendar API.
27+
Prints the start and name of the next 10 events on the user's calendar.
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('calendar', 'v3', http=creds.authorize(Http()))
4235

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'])
36+
# Call the Calendar API
37+
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
38+
print('Getting the upcoming 10 events')
39+
events_result = service.events().list(calendarId='primary', timeMin=now,
40+
maxResults=10, singleEvents=True,
41+
orderBy='startTime').execute()
42+
events = events_result.get('items', [])
43+
44+
if not events:
45+
print('No upcoming events found.')
46+
for event in events:
47+
start = event['start'].get('dateTime', event['start'].get('date'))
48+
print(start, event['summary'])
49+
50+
if __name__ == '__main__':
51+
main()
4852
# [END calendar_quickstart]

0 commit comments

Comments
 (0)