使用 MS Graph API、运行 永远上传大型邮件附件

Uploading large mail attachments with MS Graph API, running forever

我正在尝试上传大型附件 (>4 Mb)。为此,文档建议执行以下步骤:

创建消息草稿 -> 开始上传 session -> 分块上传附件 -> 发送邮件

执行第3步时,代码一直保持运行没有响应(只打印started,不打印done ).如果我理解正确,requests.put() 应该给我 NextExpectedRange 字节以馈入下一个 put 迭代。但正如我所说,它没有给我任何回应。正如您在代码中看到的那样,我尝试对块的上传过程进行硬编码。 在一些网站上,建议块长度应该被 327680 整除,这就是为什么我有这 9 次迭代。但即使尝试将整个附件作为一个块上传给我同样的(没有)反应。

弄乱 header(例如取出 Content-Length)给我一个 InvalidContentRange 错误,将 put 更改为 postContent-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"

已解决。显然我从来没有告诉它要上传哪些确切的字节 ^^