Python API客户端无法识别Reports API的“登录”类型

时间:2013-11-28 15:41:14

标签: google-api-python-client google-admin-sdk

我一直在尝试使用Python和服务帐户访问新的Reports API来下载用户登录信息。我可以成功构建服务帐户,然后尝试使用ApplicationName ='login'调用activities.list(请参阅下面的代码段)。

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

相反,我收到以下错误:

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)"

对applicationName参数使用'admin'或'docs'值而不是'login'会生成有效数据,所以我很确定我的语法正确(请参阅API参考here和Python客户参考here)。

另外,我遇​​到了Python客户端v1.1的问题,但升级到1.2后我遇到了同样的问题:easy_install --upgrade google-api-python-client

任何建议的后续步骤?

1 个答案:

答案 0 :(得分:3)

您需要替换createAuditService()函数的最后一行:

return build('reports', 'v1', http=http)

使用:

return build('admin', 'reports_v1', http=http)

以获取支持登录报告的最新版本的发现文档。

相关问题