请求电子邮件审核导出失败,状态为 400 和 "Premature end of file."

Request email audit export fails with status 400 and "Premature end of file."

根据https://developers.google.com/admin-sdk/email-audit/#creating_a_mailbox_for_export,我正在尝试以这种方式请求 G Suite 中用户的电子邮件审计导出:

def requestAuditExport(account):
    credentials = getCredentials()
    http = credentials.authorize(httplib2.Http())
    url = 'https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/helpling.com/'+account
    status, response = http.request(url, 'POST', headers={'Content-Type': 'application/atom+xml'})
    print(status)
    print(response)

我得到以下结果:

{'content-length': '22', 'expires': 'Tue, 13 Dec 2016 14:19:37 GMT', 'date': 'Tue, 13 Dec 2016 14:19:37 GMT', 'x-frame-options': 'SAMEORIGIN', 'transfer-encoding': 'chunked', 'x-xss-protection': '1; mode=block', 'content-type': 'text/html; charset=UTF-8', 'x-content-type-options': 'nosniff', '-content-encoding': 'gzip', 'server': 'GSE', 'status': '400', 'cache-control': 'private, max-age=0', 'alt-svc': 'quic=":443"; ma=2592000; v="35,34"'}

b'Premature end of file.'

我看不出问题出在哪里,有人可以给我提示吗?

提前致谢!

通过进入 Admin ConsoleManage API 客户端访问页面 Security 并添加 Directory API 所需的客户端 ID 来修复它。有关详细信息,请查看此 document

好的,找出错误并自行修复。最后看起来像这样:

http = getCredentials().authorize(httplib2.Http())
url = 'https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/helpling.com/'+account
headers = {'Content-Type': 'application/atom+xml'}
xml_data = """<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'> \
                <apps:property name='includeDeleted' value='true'/> \
              </atom:entry>"""
status, response = http.request(url, 'POST', headers=headers, body=xml_data)

不确定是关于 body 还是 header。现在可以用了,希望对其他人有帮助。

谢谢。