Skip to content

Commit 985bcd1

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 9b9471b + 4c2120d commit 985bcd1

File tree

24 files changed

+296
-294
lines changed

24 files changed

+296
-294
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ client_secret.json
33
application_credentials.json
44
storage.json
55
credentials.json
6+
token.json
67

78
.DS_Store
89

.pylintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,6 @@ max-public-methods=50
424424
# Exceptions that will emit a warning when being caught. Defaults to
425425
# "Exception"
426426
overgeneral-exceptions=Exception
427+
428+
# Python 2/3 compatibility
429+
disable=useless-object-inheritance

admin_sdk/directory/quickstart.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
from __future__ import print_function
2121
from apiclient.discovery import build
2222
from httplib2 import Http
23-
from oauth2client import file, client, tools
23+
from oauth2client import file as oauth_file, client, tools
2424

2525
# Setup the Admin SDK Directory API
2626
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
27-
store = file.Storage('credentials.json')
27+
store = oauth_file.Storage('token.json')
2828
creds = store.get()
2929
if not creds or creds.invalid:
30-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3131
creds = tools.run_flow(flow, store)
3232
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
3333

@@ -42,5 +42,6 @@
4242
else:
4343
print('Users:')
4444
for user in users:
45-
print('{0} ({1})'.format(user['primaryEmail'], user['name']['fullName']))
45+
print('{0} ({1})'.format(user['primaryEmail'],
46+
user['name']['fullName']))
4647
# [END admin_sdk_directory_quickstart]

admin_sdk/reports/quickstart.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@
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
23-
from oauth2client import file, client, tools
24-
import StringIO
25-
import random
26-
27-
import apiclient
28-
from email import Utils
29-
from email import MIMEText
23+
from oauth2client import file as oauth_file, client, tools
3024

3125
# Setup the Admin SDK Reports API
3226
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
33-
store = file.Storage('credentials.json')
27+
store = oauth_file.Storage('token.json')
3428
creds = store.get()
3529
if not creds or creds.invalid:
36-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3731
creds = tools.run_flow(flow, store)
3832
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
3933

admin_sdk/reseller/quickstart.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,16 @@
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
23-
from oauth2client import file, client, tools
24-
import StringIO
25-
import random
26-
27-
import apiclient
28-
from email import Utils
29-
from email import MIMEText
23+
from oauth2client import file as oauth_file, client, tools
3024

3125
# Setup the Admin SDK Reports API
3226
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
33-
store = file.Storage('credentials.json')
27+
store = oauth_file.Storage('token.json')
3428
creds = store.get()
3529
if not creds or creds.invalid:
36-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
30+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3731
creds = tools.run_flow(flow, store)
3832
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
3933

apps_script/execute/execute.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
# limitations under the License.
1414

1515
# [START apps_script_execute]
16+
from __future__ import print_function
17+
from apiclient import errors
18+
from apiclient.discovery import build
19+
from httplib2 import Http
20+
from oauth2client import file as oauthfile, client, tools
21+
1622
def main():
1723
"""Shows basic usage of the Apps Script API.
1824
@@ -22,10 +28,14 @@ def main():
2228
"""
2329
SCRIPT_ID = 'ENTER_YOUR_SCRIPT_ID_HERE'
2430

25-
# Authorize and create a service object.
26-
credentials = get_credentials()
27-
http = credentials.authorize(httplib2.Http())
28-
service = discovery.build('script', 'v1', http=http)
31+
# Setup the Apps Script API
32+
SCOPES = 'https://www.googleapis.com/auth/script.projects'
33+
store = oauthfile.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('script', 'v1', http=creds.authorize(Http()))
2939

3040
# Create an execution request object.
3141
request = {"function": "getFoldersUnderRoot"}

apps_script/quickstart/quickstart.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,44 @@
1919
project, and log the script's URL to the user.
2020
"""
2121
from __future__ import print_function
22+
from apiclient import errors
2223
from apiclient.discovery import build
2324
from httplib2 import Http
24-
from oauth2client import file, client, tools
25+
from oauth2client import file as oauthfile, client, tools
2526

2627
# Setup the Apps Script API
2728
SCOPES = 'https://www.googleapis.com/auth/script.projects'
28-
store = file.Storage('credentials.json')
29+
store = oauthfile.Storage('token.json')
2930
creds = store.get()
3031
if not creds or creds.invalid:
31-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
32+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3233
creds = tools.run_flow(flow, store)
3334
service = build('script', 'v1', http=creds.authorize(Http()))
3435

3536
# Call the Apps Script API
3637
try:
37-
# Create a new project
38-
request = {'title': 'My Script'}
39-
response = service.projects().create(body=request).execute()
38+
# Create a new project
39+
request = {'title2': 'My Script'}
40+
response = service.projects().create(body=request).execute()
4041

41-
# Upload two files to the project
42-
request = {
43-
'files': [{
44-
'name': 'hello',
45-
'type': 'SERVER_JS',
46-
'source': 'function helloWorld() {\n console.log("Hello, world!");\n}'
47-
}, {
48-
'name': 'appsscript',
49-
'type': 'JSON',
50-
'source': '{\"timeZone\":\"America/New_York\",\"exceptionLogging\":' + \
51-
'\"CLOUD\"}'
52-
}]
53-
}
54-
response = service.projects().updateContent(body=request,
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,
5557
scriptId=response['scriptId']).execute()
56-
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
58+
print('https://script.google.com/d/' + response['scriptId'] + '/edit')
5759
except errors.HttpError as e:
58-
# The API encountered a problem.
59-
print(e.content)
60+
# The API encountered a problem.
61+
print(e.content)
6062
# [END apps_script_quickstart]

calendar/quickstart/quickstart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
service object and outputs a list of the next 10 events on the user's calendar.
1919
"""
2020
from __future__ import print_function
21+
import datetime
2122
from apiclient.discovery import build
2223
from httplib2 import Http
23-
from oauth2client import file, client, tools
24-
import datetime
24+
from oauth2client import file as oauth_file, client, tools
2525

2626
# Setup the Calendar API
2727
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
28-
store = file.Storage('credentials.json')
28+
store = oauth_file.Storage('token.json')
2929
creds = store.get()
3030
if not creds or creds.invalid:
31-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
31+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3232
creds = tools.run_flow(flow, store)
3333
service = build('calendar', 'v3', http=creds.authorize(Http()))
3434

classroom/quickstart/quickstart.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
from __future__ import print_function
2323
from apiclient.discovery import build
2424
from httplib2 import Http
25-
from oauth2client import file, client, tools
25+
from oauth2client import file as oauth_file, client, tools
2626

2727
# Setup the Classroom API
2828
SCOPES = 'https://www.googleapis.com/auth/classroom.courses.readonly'
29-
store = file.Storage('credentials.json')
29+
store = oauth_file.Storage('token.json')
3030
creds = store.get()
3131
if not creds or creds.invalid:
32-
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
32+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
3333
creds = tools.run_flow(flow, store)
3434
service = build('classroom', 'v1', http=creds.authorize(Http()))
3535

drive/activity/quickstart.py

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,39 @@
1414

1515
# [START drive_activity_quickstart]
1616
from __future__ import print_function
17-
import httplib2
18-
import os
19-
20-
from apiclient import discovery
21-
from oauth2client import client
22-
from oauth2client import tools
23-
from oauth2client.file import Storage
24-
2517
import datetime
26-
27-
try:
28-
import argparse
29-
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
30-
except ImportError:
31-
flags = None
32-
33-
# If modifying these scopes, delete your previously saved credentials
34-
# at ~/.credentials/appsactivity-python-quickstart.json
35-
SCOPES = 'https://www.googleapis.com/auth/activity https://www.googleapis.com/auth/drive.metadata.readonly'
36-
CLIENT_SECRET_FILE = 'client_secret.json'
37-
APPLICATION_NAME = 'G Suite Activity API Python Quickstart'
38-
39-
40-
def get_credentials():
41-
"""Gets valid user credentials from storage.
42-
43-
If nothing has been stored, or if the stored credentials are invalid,
44-
the OAuth2 flow is completed to obtain the new credentials.
45-
46-
Returns:
47-
Credentials, the obtained credential.
48-
"""
49-
home_dir = os.path.expanduser('~')
50-
credential_dir = os.path.join(home_dir, '.credentials')
51-
if not os.path.exists(credential_dir):
52-
os.makedirs(credential_dir)
53-
credential_path = os.path.join(credential_dir,
54-
'appsactivity-python-quickstart.json')
55-
56-
store = Storage(credential_path)
57-
credentials = store.get()
58-
if not credentials or credentials.invalid:
59-
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
60-
flow.user_agent = APPLICATION_NAME
61-
if flags:
62-
credentials = tools.run_flow(flow, store, flags)
63-
else: # Needed only for compatibility with Python 2.6
64-
credentials = tools.run(flow, store)
65-
print('Storing credentials to ' + credential_path)
66-
return credentials
67-
68-
def main():
69-
"""Shows basic usage of the G Suite Activity API.
70-
71-
Creates a G Suite Activity API service object and
72-
outputs the recent activity in your Google Drive.
73-
"""
74-
credentials = get_credentials()
75-
http = credentials.authorize(httplib2.Http())
76-
service = discovery.build('appsactivity', 'v1', http=http)
77-
78-
results = service.activities().list(source='drive.google.com',
79-
drive_ancestorId='root', pageSize=10).execute()
80-
activities = results.get('activities', [])
81-
if not activities:
82-
print('No activity.')
83-
else:
84-
print('Recent activity:')
85-
for activity in activities:
86-
event = activity['combinedEvent']
87-
user = event.get('user', None)
88-
target = event.get('target', None)
89-
if user == None or target == None:
90-
continue
91-
time = datetime.datetime.fromtimestamp(
92-
int(event['eventTimeMillis'])/1000)
93-
print('{0}: {1}, {2}, {3} ({4})'.format(time, user['name'],
94-
event['primaryEventType'], target['name'], target['mimeType']))
95-
96-
if __name__ == '__main__':
97-
main()
18+
from apiclient.discovery import build
19+
from httplib2 import Http
20+
from oauth2client import file as oauth_file, client, tools
21+
22+
# Setup the Drive Activity API
23+
SCOPES = [
24+
'https://www.googleapis.com/auth/activity',
25+
'https://www.googleapis.com/auth/drive.metadata.readonly'
26+
]
27+
store = oauth_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('appsactivity', 'v1', http=creds.authorize(Http()))
33+
34+
# Call the Drive Activity API
35+
results = service.activities().list(source='drive.google.com',
36+
drive_ancestorId='root', pageSize=10).execute()
37+
activities = results.get('activities', [])
38+
if not activities:
39+
print('No activity.')
40+
else:
41+
print('Recent activity:')
42+
for activity in activities:
43+
event = activity['combinedEvent']
44+
user = event.get('user', None)
45+
target = event.get('target', None)
46+
if user is None or target is None:
47+
continue
48+
time = datetime.datetime.fromtimestamp(
49+
int(event['eventTimeMillis'])/1000)
50+
print('{0}: {1}, {2}, {3} ({4})'.format(time, user['name'],
51+
event['primaryEventType'], target['name'], target['mimeType']))
9852
# [END drive_activity_quickstart]

0 commit comments

Comments
 (0)