Gmail Api可恢复上传休息(附件大于5MB)

时间:2017-08-23 07:23:49

标签: rest attachment gmail-api

我正在尝试通过Gmail Api Rest发送附件大于5MB的邮件。为了实现这一点,我试图通过可恢复的上传发送它。这是我的代码。

byte[] ba = System.IO.File.ReadAllBytes(uploadFromPath);
String base64String = Convert.ToBase64String(ba);
string url = "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=resumable"
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Headers.Add("Authorization", "Bearer " + token);
request.Headers.Add("X-Upload-Content-Type", "message/rfc822");
request.Headers["X-Upload-Content-Length"]= base64String.Length.ToString();
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = body.Length;

在我提出请求后,我获得了位置

location = res.Headers["Location"];

然后我用位置发出PUT请求。

我想知道我应该在第一个请求Body中插入什么以及第二个请求中应该包含什么内容。 我看过这篇文章Attaching a file using Resumable upload w/ Gmail API 但该代码仅适用于小于5MB的文件。我还有什么办法可以完成大于5MB的连接吗?

1 个答案:

答案 0 :(得分:0)

Upload Attachment文档上实际上有样本。

第1步:启动可恢复的会话 可恢复的会话启动请求

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
}

第2步:保存可恢复的会话URI

HTTP/1.1 200 OK
Location: https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=resumable&upload_id=xa298sd_sdlkj2
Content-Length: 0

第3步:上传文件

PUT https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send?uploadType=resumable&upload_id=xa298sd_sdlkj2 HTTP/1.1
Content-Length: 2000000
Content-Type: message/rfc822

bytes 0-1999999