使用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
相关产品推荐
相关产品推荐

