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 |
| 42 | import re |
| 43 | import simplejson |
| 44 | import sys |
| 45 | |
| 46 | from apiclient.discovery import build |
| 47 | from oauth2client.client import AccessTokenRefreshError |
| 48 | from oauth2client.client import OAuth2WebServerFlow |
| 49 | from oauth2client.file import Storage |
| 50 | from oauth2client.tools import run |
| 51 | |
| 52 | FLAGS = gflags.FLAGS |
| 53 | |
| 54 | # Set up a Flow object to be used if we need to authenticate. This |
| 55 | # sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with |
| 56 | # the information it needs to authenticate. Note that it is called |
| 57 | # the Web Server Flow, but it can also handle the flow for native |
| 58 | # applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA> |
| 59 | # When creating credentials for this application be sure to choose an |
| 60 | # Application type of 'Installed application'. |
| 61 | FLOW = OAuth2WebServerFlow( |
| 62 | client_id='880851855448.apps.googleusercontent.com', |
| 63 | client_secret='d8nBjlNBpOMH_LITqz31IMdI', |
| 64 | scope='https://www.googleapis.com/auth/apps/reporting/audit.readonly', |
| 65 | user_agent='audit-cmdline-sample/1.0') |
| 66 | |
| 67 | # The flags module makes defining command-line options easy for |
| 68 | # applications. Run this program with the '--help' argument to see |
| 69 | # all the flags that it understands. |
| 70 | gflags.DEFINE_enum('logging_level', 'ERROR', |
| 71 | ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], |
| 72 | 'Set the level of logging detail.') |
| 73 | |
| 74 | |
| 75 | def print_activities(activity_list): |
| 76 | events = activity_list['items'] |
| 77 | print '\nRetrieved %d activities.' % len(events) |
| 78 | for i in range(len(events)): |
| 79 | print '\nEvent %d : %s' % (i, simplejson.JSONEncoder().encode(events[i])) |
| 80 | print '\nNext URL : %s' % (activity_list['next']) |
| 81 | print '======================================================================' |
| 82 | |
| 83 | |
| 84 | def main(argv): |
| 85 | # Let the flags module process the command-line arguments |
| 86 | try: |
| 87 | argv = FLAGS(argv) |
| 88 | except gflags.FlagsError, e: |
| 89 | print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS) |
| 90 | sys.exit(1) |
| 91 | |
| 92 | # Set the logging according to the command-line flag |
| 93 | logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level)) |
| 94 | |
| 95 | # If the Credentials don't exist or are invalid run through the native client |
| 96 | # flow. The Storage object will ensure that if successful the good |
| 97 | # Credentials will get written back to a file. |
| 98 | storage = Storage('audit.dat') |
| 99 | credentials = storage.get() |
| 100 | if not credentials or credentials.invalid: |
| 101 | credentials = run(FLOW, storage) |
| 102 | |
| 103 | # Create an httplib2.Http object to handle our HTTP requests and authorize it |
| 104 | # with our good Credentials. |
| 105 | http = httplib2.Http() |
| 106 | http = credentials.authorize(http) |
| 107 | |
| 108 | service = build('audit', 'v1', http=http) |
| 109 | |
| 110 | try: |
| 111 | activities = service.activities() |
| 112 | |
| 113 | # Retrieve the first two activities |
| 114 | print 'Retrieving the first 2 activities...' |
| 115 | activity_list = activities.list( |
| 116 | applicationId='207535951991', customerId='C01rv1wm7', maxResults='2', |
| 117 | actorEmail='admin@enterprise-audit-clientlib.com').execute() |
| 118 | print_activities(activity_list) |
| 119 | |
| 120 | # Now retrieve the next 2 events |
| 121 | match = re.search('(?<=continuationToken=).+$', activity_list['next']) |
| 122 | if match is not None: |
| 123 | next_token = match.group(0) |
| 124 | |
| 125 | print '\nRetrieving the next 2 activities...' |
| 126 | activity_list = activities.list( |
| 127 | applicationId='207535951991', customerId='C01rv1wm7', |
| 128 | maxResults='2', actorEmail='admin@enterprise-audit-clientlib.com', |
| 129 | continuationToken=next_token).execute() |
| 130 | print_activities(activity_list) |
| 131 | |
| 132 | except AccessTokenRefreshError: |
| 133 | print ('The credentials have been revoked or expired, please re-run' |
| 134 | 'the application to re-authorize') |
| 135 | |
| 136 | if __name__ == '__main__': |
| 137 | main(sys.argv) |