我正在嘗試上傳大型附件(> 4 Mb)。為此,檔案建議采取以下步驟:
創建草稿訊息 -> 開始上傳會話 -> 分塊上傳附件 -> 發送郵件
執行第 3 步時,代碼一直運行而沒有回應(僅started列印,但不列印done)。如果我理解正確,requests.put()應該給我NextExpectedRange位元組以供下put一次迭代。但正如我所說,它沒有給我任何回應。正如您在代碼中看到的那樣,我嘗試對塊的上傳程序進行硬編碼。在某些站點中,建議塊長度應該可以除以327680,這就是我有這 9 次迭代的原因。但即使嘗試將整個附件作為一個塊上傳,也會給我同樣的(不)反應。
弄亂標題(例如取出Content-Length)會給我一個InvalidContentRange錯誤,更改put為post或Content-Type給我相同的(不)反應。
不用說,我擁有所有必需的權限。
將不勝感激任何形式的幫助:)
#1. create draft
payload=json.dumps({
"subject":subject,
"importance":"Low",
"body":{
"contentType":"Text",
"content":content},
"toRecipients":setup_recipients(recipients),
#"ccRecipients":setup_cc_recipients(ccrecipients)
})
mail_data_draft = requests.post(url=ms_graph_endpoint_draft, data=payload, headers={'Authorization': 'Bearer ' result['access_token'], 'Content-Type': 'application/json'})
message_id=json.loads(mail_data_draft.text)["id"]
#2. creating upload session with message id
upload_session_endpoint="https://graph.microsoft.com/v1.0/users/" email_account "/messages/" message_id "/attachments/createUploadSession"
up_session_payload= json.dumps({
"AttachmentItem":{
"attachmentType":"file",
"name":attachmentName,
"size": contentSize
}
})
upload_session_info= requests.post(url=upload_session_endpoint, data=up_session_payload, headers={'Authorization': 'Bearer ' result['access_token'], 'Content-Type': 'application/json'})
opaque_url =json.loads(upload_session_info.text)["uploadUrl"]
next_expected_range=json.loads(upload_session_info.text)["nextExpectedRanges"][0]
#####################################################################Tested till here
#3. uploading attachment
#problem
print("started")
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 0-327679/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 327680-655359/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 655360-983039/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 983040-1310719/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 1310720-1638399/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 16383400-1966079/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 1966080-2293759/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 2293760-2621439/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 2621440-2949119/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'283842','Content-Range':'bytes 2949120-3232961/3232962'})
print("done")
# sending draft
global ms_graph_endpoint
ms_graph_endpoint="https://graph.microsoft.com/v1.0/users/" email_account "/messages/" message_id "/send"
uj5u.com熱心網友回復:
解決了。顯然我從來沒有告訴它要上傳哪些確切的位元組^^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/461140.html
標籤:Python http 蟒蛇请求 微软图形 API 微软图形邮件
