【问题标题】:How to use G Suite Email Audit API with google-api-python-client?如何将 G Suite 电子邮件审核 API 与 google-api-python-client 一起使用?
【发布时间】:2018-03-02 20:15:51
【问题描述】:

我想获取使用 google-api-python-client 的特定用户发送的电子邮件列表(主题、日期时间、发件人)。

我找到了G Suite Email Audit API Developer's Guide,但不幸的是没有任何 python 示例。

google-api-python-client 甚至可以做到这一点吗?

【问题讨论】:

    标签: python api audit google-api-python-client google-workspace


    【解决方案1】:

    G Suite Email Audit API 是仍然使用Google Data API protocol 的旧 API 之一。 google-api-python-client 不支持此协议,但您必须改用 gdata-python-client。这个库很旧,Google 不再更新它:

    • 它仅适用于 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 = '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)
    

    如果您想要 Google 的原始示例,可以找到 here。它比我的复杂得多,我怀疑它不会工作,因为它没有执行 OAuth2 身份验证;作为参考。

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 2015-09-20
      • 2020-05-27
      相关资源
      最近更新 更多