rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 4 | # Copyright 2014 Google Inc. All Rights Reserved. |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 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 | |
| 20 | Command-line application that retrieves events through the Audit API. |
| 21 | This works only for Google Apps for Business, Education, and ISP accounts. |
| 22 | It can not be used for the basic Google Apps product. |
| 23 | |
| 24 | Usage: |
| 25 | $ python audit.py |
| 26 | |
| 27 | You can also get help on all the command-line flags the program understands |
| 28 | by running: |
| 29 | |
| 30 | $ python audit.py --help |
| 31 | |
| 32 | To get detailed log output run: |
| 33 | |
| 34 | $ python audit.py --logging_level=DEBUG |
| 35 | """ |
| 36 | |
| 37 | __author__ = 'rahulpaul@google.com (Rahul Paul)' |
| 38 | |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 39 | import pprint |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 40 | import sys |
| 41 | |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 42 | from oauth2client import client |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 43 | from googleapiclient import sample_tools |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def main(argv): |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 47 | # Authenticate and construct service. |
| 48 | service, flags = sample_tools.init( |
| 49 | argv, 'audit', 'v1', __doc__, __file__, |
| 50 | scope='https://www.googleapis.com/auth/apps/reporting/audit.readonly') |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 51 | |
| 52 | service = build('audit', 'v1', http=http) |
| 53 | |
| 54 | try: |
| 55 | activities = service.activities() |
| 56 | |
| 57 | # Retrieve the first two activities |
| 58 | print 'Retrieving the first 2 activities...' |
| 59 | activity_list = activities.list( |
| 60 | applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', |
| 61 | actorEmail='admin@enterprise-audit-clientlib.com').execute() |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 62 | pprint.pprint(activity_list) |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 63 | |
| 64 | # Now retrieve the next 2 events |
| 65 | match = re.search('(?<=continuationToken=).+$', activity_list['next']) |
| 66 | if match is not None: |
| 67 | next_token = match.group(0) |
| 68 | |
| 69 | print '\nRetrieving the next 2 activities...' |
| 70 | activity_list = activities.list( |
| 71 | applicationId='207535951991', customerId='C01rv1wm7', |
| 72 | maxResults='2', actorEmail='admin@enterprise-audit-clientlib.com', |
| 73 | continuationToken=next_token).execute() |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 74 | pprint.pprint(activity_list) |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 75 | |
Joe Gregorio | 652492b | 2013-06-30 23:19:25 -0400 | [diff] [blame] | 76 | except client.AccessTokenRefreshError: |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 77 | print ('The credentials have been revoked or expired, please re-run' |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 78 | 'the application to re-authorize') |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 79 | |
| 80 | if __name__ == '__main__': |
| 81 | main(sys.argv) |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 82 | |