1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3# 4# Copyright 2014 Google Inc. All Rights Reserved. 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18"""Simple command-line sample for Audit API. 19 20Command-line application that retrieves events through the Audit API. 21This works only for Google Apps for Business, Education, and ISP accounts. 22It can not be used for the basic Google Apps product. 23 24Usage: 25 $ python audit.py 26 27You can also get help on all the command-line flags the program understands 28by running: 29 30 $ python audit.py --help 31 32To get detailed log output run: 33 34 $ python audit.py --logging_level=DEBUG 35""" 36from __future__ import print_function 37 38__author__ = 'rahulpaul@google.com (Rahul Paul)' 39 40import pprint 41import sys 42 43from oauth2client import client 44from googleapiclient import sample_tools 45 46 47def main(argv): 48 # Authenticate and construct service. 49 service, flags = sample_tools.init( 50 argv, 'audit', 'v1', __doc__, __file__, 51 scope='https://www.googleapis.com/auth/apps/reporting/audit.readonly') 52 53 try: 54 activities = service.activities() 55 56 # Retrieve the first two activities 57 print('Retrieving the first 2 activities...') 58 activity_list = activities.list( 59 applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', 60 actorEmail='admin@enterprise-audit-clientlib.com').execute() 61 pprint.pprint(activity_list) 62 63 # Now retrieve the next 2 events 64 match = re.search('(?<=continuationToken=).+$', activity_list['next']) 65 if match is not None: 66 next_token = match.group(0) 67 68 print('\nRetrieving the next 2 activities...') 69 activity_list = activities.list( 70 applicationId='207535951991', customerId='C01rv1wm7', 71 maxResults='2', actorEmail='admin@enterprise-audit-clientlib.com', 72 continuationToken=next_token).execute() 73 pprint.pprint(activity_list) 74 75 except client.AccessTokenRefreshError: 76 print ('The credentials have been revoked or expired, please re-run' 77 'the application to re-authorize') 78 79if __name__ == '__main__': 80 main(sys.argv) 81 82