blob: 1595ddbd0a00ed4b9150891c361d8eb3bf7cea40 [file] [log] [blame]
Craig Citro15744b12015-03-02 13:34:32 -08001#!/usr/bin/env python
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +00002# -*- coding: utf-8 -*-
3#
Craig Citro751b7fb2014-09-23 11:20:38 -07004# Copyright 2014 Google Inc. All Rights Reserved.
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +00005#
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"""
INADA Naokie8d87822014-08-20 15:25:24 +090036from __future__ import print_function
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000037
38__author__ = 'rahulpaul@google.com (Rahul Paul)'
39
Joe Gregorio84e41802012-06-20 12:10:56 -040040import pprint
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000041import sys
42
Joe Gregorio652492b2013-06-30 23:19:25 -040043from oauth2client import client
John Asmuth864311d2014-04-24 15:46:08 -040044from googleapiclient import sample_tools
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000045
46
47def main(argv):
Joe Gregorio652492b2013-06-30 23:19:25 -040048 # 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')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000052
53 service = build('audit', 'v1', http=http)
54
55 try:
56 activities = service.activities()
57
58 # Retrieve the first two activities
INADA Naokie8d87822014-08-20 15:25:24 +090059 print('Retrieving the first 2 activities...')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000060 activity_list = activities.list(
61 applicationId='207535951991', customerId='C01rv1wm7', maxResults='2',
62 actorEmail='admin@enterprise-audit-clientlib.com').execute()
Joe Gregorio652492b2013-06-30 23:19:25 -040063 pprint.pprint(activity_list)
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000064
65 # Now retrieve the next 2 events
66 match = re.search('(?<=continuationToken=).+$', activity_list['next'])
67 if match is not None:
68 next_token = match.group(0)
69
INADA Naokie8d87822014-08-20 15:25:24 +090070 print('\nRetrieving the next 2 activities...')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000071 activity_list = activities.list(
72 applicationId='207535951991', customerId='C01rv1wm7',
73 maxResults='2', actorEmail='admin@enterprise-audit-clientlib.com',
74 continuationToken=next_token).execute()
Joe Gregorio652492b2013-06-30 23:19:25 -040075 pprint.pprint(activity_list)
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000076
Joe Gregorio652492b2013-06-30 23:19:25 -040077 except client.AccessTokenRefreshError:
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000078 print ('The credentials have been revoked or expired, please re-run'
Joe Gregorio84e41802012-06-20 12:10:56 -040079 'the application to re-authorize')
rahulpaul@google.com4bd78e22011-08-18 05:37:47 +000080
81if __name__ == '__main__':
82 main(sys.argv)
Joe Gregorio84e41802012-06-20 12:10:56 -040083