2017-09-21 134 views

回答

2

G Suite Email Audit API是仍然使用Google Data API protocol的舊版API之一。這個協議不被google-api-python-client支持,但你必須使用gdata-python-client。這個庫是舊的,谷歌是不會再更新它:

  • 它只與Python 2.x的工作
  • 執行的OAuth2認證,你需要將它與oauth2client

這裏是整合關於如何使用它的示例:

from __future__ import print_function 

import argparse 

import gdata.apps.audit.service 
from oauth2client import file, client, tools 

SCOPES = ['https://apps-apis.google.com/a/feeds/compliance/audit/',] 

# be sure to update with the correct user 
ID = '[email protected]' 

store = file.Storage('email-audit{}.json'.format(ID)) 
creds = store.get() 

# client_id.json is the client_id file generated from the developer console project 
if not creds or creds.invalid: 
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() 
    flags.auth_host_port = [8010, 8020] 
    flow = client.flow_from_clientsecrets('client_id.json', SCOPES) 
    creds = tools.run_flow(flow, store, flags) 

access_token, expires_in = creds.get_access_token() 

gd_client = gdata.apps.audit.service.AuditService(domain=ID.split('@')[1]) 
gd_client.additional_headers[u'Authorization'] = u'Bearer {0}'.format(access_token) 

monitors = gd_client.getEmailMonitors(ID.split('@')[0]) 

print(monitors) 

如果你想從谷歌的原始樣品,你可以找到它here。它比我的複雜得多,我懷疑它會工作,因爲它不執行OAuth2身份驗證;用它作爲參考。