Gmail Api断点续传上传(附件大于5MB) [英] Gmail Api resumable upload Rest( attachment larger than 5MB)

查看:111
本文介绍了Gmail Api断点续传上传(附件大于5MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Gmail Api Rest发送附件大于5MB的邮件.为此,我正在尝试通过可恢复的上传发送它.这是我的代码.

I am trying to send via Gmail Api Rest a mail with attachment larger than 5MB. To accomplish that I am trying to sent it with resumable upload. This is my code.

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;

发出请求后,我就知道了位置

After I make the request I am getting the location

location = res.Headers["Location"];

之后,我会发出带有该位置的PUT请求.

and after that I make the PUT request with the location.

我想知道我应该在第一个请求正文中插入什么内容,以及在第二个请求中插入什么内容. 我看过这篇文章使用可恢复上传功能(通过Gmail)附加文件API 但是该代码仅适用于小于5MB的文件.要完成大于5MB的任务,我还需要做些其他事情吗?

I would like to know what should I insert inside first request Body and what should be inside second request. I have seen this post Attaching a file using Resumable upload w/ Gmail API but the code worked only for files smaller than 5MB. Is there anything else I should do to accomplish attchment larger than 5MB?

推荐答案

第1步:开始可恢复的会话 可恢复的会话发起请求

Step 1: Start a resumable session Resumable session initiation request

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

这篇关于Gmail Api断点续传上传(附件大于5MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆