【问题标题】:gmail REST api 'import message' with python requests带有python请求的gmail REST api“导入消息”
【发布时间】:2016-11-05 04:49:51
【问题描述】:

我正在尝试使用 gmail REST API 将电子邮件上传到 gmail 帐户,以使用 python 请求模块导入消息https://developers.google.com/gmail/api/v1/reference/users/messages/import#http-request。电子邮件是带有正文的 RAW 格式(即用 base64.b64encode() 编码的 .eml 文件的数据)

POST https://www.googleapis.com/gmail/v1/users/me/messages/import
{
 "raw": ".........."
}

这适用于最大为几 MB(例如 5MB)的 RAW 数据,但如果我尝试上传更大的数据(~10 MB 或更多)。

谁能告诉我如何使用此 API 上传更大的电子邮件数据。

我正在使用 OAuth access_token 调用这些 API

【问题讨论】:

    标签: python-2.7 import python-requests gmail-api eml


    【解决方案1】:

    尝试将 uploadType 设置为 resumable

    Resumable upload

    要更可靠地上传数据文件,您可以使用可恢复上传协议。此协议允许您在通信故障中断数据流后恢复上传操作。如果您正在传输大文件并且网络中断或其他一些传输失败的可能性很高,它特别有用,例如,当从移动客户端应用程序上传。它还可以在发生网络故障时减少您的带宽使用,因为您不必从头开始重新启动大文件上传。

    示例:可恢复会话发起请求

    POST /upload/gmail/v1/users/userId/messages/send?uploadType=resumable HTTP/1.1
    Host: www.googleapis.com
    Authorization: Bearer your_auth_token
    Content-Length: 38
    Content-Type: application/json; charset=UTF-8
    X-Upload-Content-Type: message/rfc822
    X-Upload-Content-Length: 2000000
    
    {
    "id": string,
    "threadId": string,
    "labelIds": [
    string
    ],
    "snippet": string,
    "historyId": unsigned long,
    "payload": {
    "partId": string,
    "mimeType": string,
    "filename": string,
    "headers": [
    {
    "name": string,
    "value": string
    }
    ],
    "body": users.messages.attachments Resource,
    "parts": [
    (MessagePart)
    ]
    },
    "sizeEstimate": integer,
    "raw": bytes
    }
    

    【讨论】:

    • @mr-rebot 感谢您的示例,我不是尝试发送邮件,而是尝试使用 RAW eml 流恢复/导入现有邮件。使用 /upload 和 uploadType=resumable 导入时,我收到错误 {u'error': {u'code': 400, u'message': u"Expected a single 'From' header", u'errors' : [{u'domain': u'global', u'message': u"期望单个 'From' 标头", u'reason': u'invalidArgument'}]}}
    • @mr-rebot,我试过这个例子(发送邮件)。它返回给我状态码为 200 的空响应。我期待像下面这样的响应,但变得空了。即没有 location/session_uri 响应。 HTTP/1.1 200 OK 位置:googleapis.com/upload/gmail/v1/users/userId/messages/… 内容长度:0
    猜你喜欢
    • 2014-09-22
    • 2018-02-17
    • 1970-01-01
    • 2020-07-12
    • 2015-11-28
    • 2020-05-14
    • 2018-06-04
    • 2018-07-23
    • 1970-01-01
    相关资源
    最近更新 更多