Skip to content

Commit 503c63f

Browse files
authored
Import activity quickstart
1 parent 6982c27 commit 503c63f

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

drive/activity/quickstart.py

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

0 commit comments

Comments
 (0)