2017-08-23 35 views
1

我試圖通過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的連接?

回答

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 
+0

如何初始化有效負載正文和部分?如果我在內部有效負載中設置附件的數據,http身體變得太大,我得到一個錯誤的請求錯誤。你有任何一個初始化的http身體的實際例子?在第三步PUT請求中還有更多內容是否只輸入在base64上轉換的附件字節? @noogui – kostas