python - Python Graph API 请求仅从电子邮件中检索 1 个附件
问题描述
我正在使用 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 个项目。
解决方案
下面的代码检索多个附件(文件是附件名称的数组)
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)
推荐阅读
- java - 如何为我的数独游戏制作 GUI?(摇摆)
- javascript - React Hooks 实现计数器
- cmd - 无法在 bash/cmd 中从 Windows 10 启动到 kerberos 服务器 rhel 7
- java - 无法访问类静态元素
- python - 这个算法可以反转吗?
- java - Apache POI在Excel中用箭头绘制线条形状从到
- python-3.x - 如何使用 python 从链表中完全删除内存中的节点?
- azure - 在 azure 上的 docker compose 文件上指定帐户凭据
- javascript - 如何根据类名和内部文本更改元素的背景?
- database - 在 Cassandra 中删除读取和删除突变