代码之家  ›  专栏  ›  技术社区  ›  user7141836

如何在google API python客户端上使用G套件电子邮件审计API?

  •  4
  • user7141836  · 技术社区  · 7 年前

    google-api-python-client .

    我找到了 G Suite Email Audit API Developer's Guide

    这有可能吗 google api python客户端

    1 回复  |  直到 7 年前
        1
  •  1
  •   Lorenzo Persichetti    7 年前

    这个 G Suite Email Audit API Google Data API protocol . google api python客户端不支持该协议,但您必须使用 gdata-python-client . 这个图书馆很旧,谷歌不再更新它:

    • 要执行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 = 'user@domain.com'
    
    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身份验证;将其用作参考。