I've been trying to use Python and a service account to access the new Reports API to download user login information. I can successfully build a service account, and then I try to call activities.list using ApplicationName='login' (see snippet below).
def createAuditService():
f = file(my-private-key, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(my-service-account, key,
scope='https://www.googleapis.com/auth/admin.reports.audit.readonly')
http = httplib2.Http()
http = credentials.authorize(http)
return build('reports', 'v1', http=http)
def retrieveActivites(service):
try:
Logins = service.activities().list(userKey='all',applicationName='login').execute()
return Logins.get('items',[])
except errors.HttpError, error:
log.error('An error occurred.',exc_info=True)
return None
def main():
service = createAuditService()
activities = retrieveActivites(service)
print activities
Instead I get the following error:
2013-11-28 09:51:17,727 - apiclient.discovery - INFO - URL being requested: https://www.googleapis.com/discovery/v1/apis/reports/v1/rest
2013-11-28 09:51:17,727 - oauth2client.client - INFO - Attempting refresh to obtain initial access_token
2013-11-28 09:51:18,135 - oauth2client.client - INFO - Refreshing access_token
Traceback (most recent call last):
File "F:\Dropbox\code\glogin-audit\glogin.py", line 105, in <module>
main()
File "F:\Dropbox\code\glogin-audit\glogin.py", line 91, in main
activities = retrieveActivites(service)
File "F:\Dropbox\code\glogin-audit\glogin.py", line 75, in retrieveActivites
Logins = service.activities().list(userKey='all',applicationName='login').execute()
File "build\bdist.win32\egg\apiclient\discovery.py", line 595, in method
(name, pvalue, regex))
TypeError: Parameter "applicationName" value "login" does not match the pattern "(admin)|(docs)"
Using 'admin' or 'docs' values instead of 'login' for the applicationName parameter produces valid data, so I'm fairly sure I have the syntax right (see the API reference here, and the Python client reference here).
Also, I was having the problem with the Python client v1.1, but I'm having the same problem after I upgraded to 1.2: easy_install --upgrade google-api-python-client
Any suggested next steps?
You need to replace the last line of your createAuditService() function:
return build('reports', 'v1', http=http)
with:
return build('admin', 'reports_v1', http=http)
in order to get the latest version of the discovery document which supports login reports.