|
| 1 | +# Copyright 2018 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START drive_activity_quickstart] |
| 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 | +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() |
| 98 | +# [END drive_activity_quickstart] |
0 commit comments