rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright (C) 2011 Google Inc. |
| 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 | |
| 39 | import gflags |
| 40 | import httplib2 |
| 41 | import logging |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 42 | import os |
| 43 | import pprint |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 44 | import sys |
| 45 | |
| 46 | from apiclient.discovery import build |
| 47 | from oauth2client.client import AccessTokenRefreshError |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 48 | from oauth2client.client import flow_from_clientsecrets |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 49 | from oauth2client.file import Storage |
| 50 | from oauth2client.tools import run |
| 51 | |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 52 | |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 53 | FLAGS = gflags.FLAGS |
| 54 | |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 55 | # CLIENT_SECRETS, name of a file containing the OAuth 2.0 information for this |
| 56 | # application, including client_id and client_secret, which are found |
| 57 | # on the API Access tab on the Google APIs |
| 58 | # Console <http://code.google.com/apis/console> |
| 59 | CLIENT_SECRETS = 'client_secrets.json' |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 60 | |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 61 | # Helpful message to display in the browser if the CLIENT_SECRETS file |
| 62 | # is missing. |
| 63 | MISSING_CLIENT_SECRETS_MESSAGE = """ |
| 64 | WARNING: Please configure OAuth 2.0 |
| 65 | |
| 66 | To make this sample run you will need to populate the client_secrets.json file |
| 67 | found at: |
| 68 | |
| 69 | %s |
| 70 | |
| 71 | with information from the APIs Console <https://code.google.com/apis/console>. |
| 72 | |
| 73 | """ % os.path.join(os.path.dirname(__file__), CLIENT_SECRETS) |
| 74 | |
| 75 | # Set up a Flow object to be used if we need to authenticate. |
| 76 | FLOW = flow_from_clientsecrets(CLIENT_SECRETS, |
| 77 | scope='https://www.googleapis.com/auth/apps/reporting/audit.readonly', |
| 78 | message=MISSING_CLIENT_SECRETS_MESSAGE) |
| 79 | |
| 80 | |
| 81 | # The gflags module makes defining command-line options easy for |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 82 | # applications. Run this program with the '--help' argument to see |
| 83 | # all the flags that it understands. |
| 84 | gflags.DEFINE_enum('logging_level', 'ERROR', |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 85 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 86 | 'Set the level of logging detail.') |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | def main(argv): |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 90 | # Let the gflags module process the command-line arguments |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 91 | try: |
| 92 | argv = FLAGS(argv) |
| 93 | except gflags.FlagsError, e: |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 94 | print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS) |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 95 | sys.exit(1) |
| 96 | |
| 97 | # Set the logging according to the command-line flag |
| 98 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 99 | |
| 100 | # If the Credentials don't exist or are invalid run through the native client |
| 101 | # flow. The Storage object will ensure that if successful the good |
| 102 | # Credentials will get written back to a file. |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 103 | storage = Storage('plus.dat') |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 104 | credentials = storage.get() |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 105 | |
| 106 | if credentials is None or credentials.invalid: |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 107 | credentials = run(FLOW, storage) |
| 108 | |
| 109 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 110 | # with our good Credentials. |
| 111 | http = httplib2.Http() |
| 112 | http = credentials.authorize(http) |
| 113 | |
| 114 | service = build('audit', 'v1', http=http) |
| 115 | |
| 116 | try: |
| 117 | activities = service.activities() |
| 118 | |
| 119 | # Retrieve the first two activities |
| 120 | print 'Retrieving the first 2 activities...' |
| 121 | activity_list = activities.list( |
| 122 | applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', |
| 123 | actorEmail='admin@enterprise-audit-clientlib.com').execute() |
| 124 | print_activities(activity_list) |
| 125 | |
| 126 | # Now retrieve the next 2 events |
| 127 | match = re.search('(?<=continuationToken=).+$', activity_list['next']) |
| 128 | if match is not None: |
| 129 | next_token = match.group(0) |
| 130 | |
| 131 | print '\nRetrieving the next 2 activities...' |
| 132 | activity_list = activities.list( |
| 133 | applicationId='207535951991', customerId='C01rv1wm7', |
| 134 | maxResults='2', actorEmail='admin@enterprise-audit-clientlib.com', |
| 135 | continuationToken=next_token).execute() |
| 136 | print_activities(activity_list) |
| 137 | |
| 138 | except AccessTokenRefreshError: |
| 139 | print ('The credentials have been revoked or expired, please re-run' |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 140 | 'the application to re-authorize') |
rahulpaul@google.com | 4bd78e2 | 2011-08-18 05:37:47 +0000 | [diff] [blame] | 141 | |
| 142 | if __name__ == '__main__': |
| 143 | main(sys.argv) |
Joe Gregorio | 84e4180 | 2012-06-20 12:10:56 -0400 | [diff] [blame] | 144 | |