首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Graph API请求仅从电子邮件中检索1个附件

Python Graph API请求仅从电子邮件中检索1个附件
EN

Stack Overflow用户
提问于 2021-02-09 09:52:28
回答 1查看 198关注 0票数 0

我正在使用python脚本发送API请求,以获取电子邮件的附件。我使用的电子邮件有4个附件(加上电子邮件签名中的图片)。python请求只从签名中检索1个附件和图片。当使用带有完全相同信息的Postman时,它会检索所有附件以及图片。

有没有办法让我拿到其他附件?

代码语言:javascript
复制
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个项目。

EN

回答 1

Stack Overflow用户

发布于 2021-02-16 11:21:34

下面的代码检索多个附件(文件是附件名称的数组)

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66111888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档