|
14 | 14 |
|
15 | 15 | # [START drive_activity_quickstart] |
16 | 16 | 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 | | - |
25 | 17 | 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'])) |
98 | 52 | # [END drive_activity_quickstart] |
0 commit comments