blob: c34de84ea4b36f5729dece66b1e51a6f3cb9fe49 [file] [log] [blame]
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +00001#!/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
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"""
36
37__author__ = 'rahulpaul@google.com (Rahul Paul)'
38
39import gflags
40import httplib2
41import logging
Joe Gregorio84e41802012-06-20 12:10:56 -040042import os
43import pprint
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000044import sys
45
46from apiclient.discovery import build
47from oauth2client.client import AccessTokenRefreshError
Joe Gregorio84e41802012-06-20 12:10:56 -040048from oauth2client.client import flow_from_clientsecrets
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000049from oauth2client.file import Storage
50from oauth2client.tools import run
51
Joe Gregorio84e41802012-06-20 12:10:56 -040052
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000053FLAGS = gflags.FLAGS
54
Joe Gregorio84e41802012-06-20 12:10:56 -040055# 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>
59CLIENT_SECRETS = 'client_secrets.json'
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000060
Joe Gregorio84e41802012-06-20 12:10:56 -040061# Helpful message to display in the browser if the CLIENT_SECRETS file
62# is missing.
63MISSING_CLIENT_SECRETS_MESSAGE = """
64WARNING: Please configure OAuth 2.0
65
66To make this sample run you will need to populate the client_secrets.json file
67found at:
68
69 %s
70
71with 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.
76FLOW = 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.com4bd78e22011-08-18 05:37:47 +000082# applications. Run this program with the '--help' argument to see
83# all the flags that it understands.
84gflags.DEFINE_enum('logging_level', 'ERROR',
Joe Gregorio84e41802012-06-20 12:10:56 -040085 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
86 'Set the level of logging detail.')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000087
88
89def main(argv):
Joe Gregorio84e41802012-06-20 12:10:56 -040090 # Let the gflags module process the command-line arguments
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000091 try:
92 argv = FLAGS(argv)
93 except gflags.FlagsError, e:
Joe Gregorio84e41802012-06-20 12:10:56 -040094 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000095 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 Gregorio84e41802012-06-20 12:10:56 -0400103 storage = Storage('plus.dat')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +0000104 credentials = storage.get()
Joe Gregorio84e41802012-06-20 12:10:56 -0400105
106 if credentials is None or credentials.invalid:
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +0000107 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 Gregorio84e41802012-06-20 12:10:56 -0400140 'the application to re-authorize')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +0000141
142if __name__ == '__main__':
143 main(sys.argv)
Joe Gregorio84e41802012-06-20 12:10:56 -0400144