【问题标题】:Gmail Api resumable upload Rest( attachment larger than 5MB)Gmail Api 可恢复上传 Rest(附件大于 5MB)
【发布时间】:2018-01-31 15:47:36
【问题描述】:

我正在尝试通过 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 请求。

我想知道我应该在第一个请求正文中插入什么以及应该在第二个请求中插入什么。 我看过这个帖子Attaching a file using Resumable upload w/ Gmail API 但该代码仅适用于小于 5MB 的文件。我还应该做些什么来完成大于 5MB 的附件?

【问题讨论】:

    标签: rest attachment gmail-api


    【解决方案1】:

    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
    

    【讨论】:

    • 如何初始化有效载荷主体和部分?如果我在有效负载正文中设置附件的数据,http 正文会变得太大,并且我收到错误的请求错误。你有初始化http主体的实际例子吗?在第 3 步 PUT 请求中,我是否只输入在 base64 上转换的附件字节? @noogui
    猜你喜欢
    • 2018-01-16
    • 2017-06-11
    • 2016-05-29
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2016-10-06
    相关资源
    最近更新 更多