Skip to content

Commit 9c7bef4

Browse files
author
Eric Koleda
committed
Also update the credentail file names for the Drive Activity API quickstart, and bring it up to date.
1 parent 6472b70 commit 9c7bef4

1 file changed

Lines changed: 29 additions & 78 deletions

File tree

drive/activity/quickstart.py

Lines changed: 29 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,85 +14,36 @@
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-
17+
from apiclient.discovery import build
18+
from httplib2 import Http
19+
from oauth2client import file, client, tools
2520
import datetime
2621

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
22+
# Setup the Drive Activity API
3523
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()
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()))
30+
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']))
9849
# [END drive_activity_quickstart]

0 commit comments

Comments
 (0)