You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python调用Microsoft Graph API按收件日期过滤邮件报错求助

问题:通过Microsoft Graph API按ReceivedDateTime筛选邮件失败

我写了个Python脚本,想用ReceivedDateTime条件检索指定用户的邮件,但运行时一直报错。错误截图如下:
错误截图

完整脚本代码:

import msal
import json
import requests

def get_access_token():
    tenantID = 'yyy'
    authority = 'https://login.microsoftonline.com/' + tenantID
    clientID = 'xxx'
    clientSecret = 'xxx'
    scope = ['https://graph.microsoft.com/.default']
    app = msal.ConfidentialClientApplication(clientID, authority=authority, client_credential = clientSecret)
    access_token = app.acquire_token_for_client(scopes=scope)
    return access_token

# token block
access_token = get_access_token()
token = access_token['access_token']


# Set the parameters for the email search
# received 2023-01-22T21:13:24Z
date_received = '2023-01-22T21:13:24Z'
mail_subject = 'Test Mail'
mail_sender = 'bernardberbell@gmail.com'
mailuser = 'bernardmwanza@bernardcomms.onmicrosoft.com'

# Construct the URL for the Microsoft Graph API
url = "https://graph.microsoft.com/v1.0/users/{}/Messages?$filter=(ReceivedDateTime ge '{}')&$select=subject,from".format(mailuser, date_received)

# Set the headers for the API call
headers = {
    "Authorization": f"Bearer {token}",
    "Content-Type": "application/json"
}

# Send the API request and get the response
response = requests.get(url, headers=headers)

print(response)

# Parse the response as JSON
data = json.loads(response.text)

print(data)

我尝试把API请求URL改成下面这种格式,还是报同样的错,请问哪里操作错了?

url = "https://graph.microsoft.com/v1.0/users/{}/Messages?$filter=ReceivedDateTime ge '{}'&$select=subject,from".format(mailuser, date_received)

内容的提问来源于stack exchange,提问作者Bernietechy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:35:21