Gmail Api可恢复上传Rest(附件大于5MB)
问题描述:
我试图通过Gmail发送Api Api其他邮件附件大于5MB。为了达到这个目标,我试图用可恢复的上传来发送它。这是我的代码。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的连接?
答
实际上有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身体的实际例子?在第三步PUT请求中还有更多内容是否只输入在base64上转换的附件字节? @noogui – kostas