我正在使用python脚本发送API请求,以获取电子邮件的附件。我使用的电子邮件有4个附件(加上电子邮件签名中的图片)。python请求只从签名中检索1个附件和图片。当使用带有完全相同信息的Postman时,它会检索所有附件以及图片。
有没有办法让我拿到其他附件?
import requests
url = 'https://graph.microsoft.com/v1.0/users/{{users email}}/messages/{{messageID}}/attachments'
body = None
head = {"Content-Type": "application/json;charset=UFT-8", "Authorization": "Bearer " + accessToken}
response1 = requests.get(url, data=body, headers=head)
response = response1.text下面显示了来自python脚本的响应,其中只有7个项目,以及Postman响应中的10个项目。


发布于 2021-02-16 11:21:34
下面的代码检索多个附件(文件是附件名称的数组)
def execute(accessToken, messageID, files, noAttachments):
import os
from os import path
import requests
import base64
import json
if noAttachments == "False":
url = 'https://graph.microsoft.com/v1.0/users/{{users email}}/messages/{{messageID}}/attachments'
body = {}
head = {"Authorization": "Bearer " + accessToken}
responseCode = requests.request("GET", url, headers=head, data = body)
response = responseCode.text
test = json.loads(responseCode.text.encode('utf8'))
x, contentBytes = response.split('"contentBytes":"',1)
if len(files) == 1:
imgdata = base64.b64decode(str(contentBytes))
filename = "C:/Temp/SAPCareAttachments/" + files[0]
with open(filename, 'wb') as f:
f.write(imgdata)
else:
for file in test["value"]:
imgdata = base64.b64decode(file["contentBytes"])
if file["name"] in files:
filename = "C:/Temp/" + file["name"]
with open(filename, 'wb') as f:
f.write(imgdata)
print(responseCode)https://stackoverflow.com/questions/66111888
复制相似问题