我想阅读某些电子邮件并过滤它们。我正在使用 Microsoft Graph API 查询 Office 365 邮箱,如下所示。我还添加了 API 权限“Mail.ReadBasic.All”,用于从应用程序获取邮件。
graph_api_endpoint = 'https://graph.microsoft.com/v1.0/users/[email protected]/mailFolders/Inbox/messages?$select=body'
# Function to get inbox messages
def get_inbox_messages(access_token):
headers = {
'Authorization': f'Bearer {access_token}',
'Accept': 'application/json',
'Prefer': 'outlook.body-content-type="text"',
}
response = requests.get(graph_api_endpoint, headers=headers)
print('get_inbox_messages response:', response)
return response.json().get('value', [])
# MSAL ConfidentialClientApplication
app = ConfidentialClientApplication(
client_id,
authority=authority,
client_credential=client_secret,
)
token_response = app.acquire_token_for_client(scopes=[scope])
# Access token
access_token = token_response['access_token']
print(f"Access Token: {access_token}")
if access_token:
inbox_messages = get_inbox_messages(access_token)
print('Inbox Messages:', inbox_messages)
else:
print('Failed to obtain access token')
但是 inbox_messages 不包含正文字段。它的价值是这样的。
[{'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACbgpYA"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACb9YkJAAA='}, {'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACbgpAa"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACb9YkIAAA='}, {'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACazuV6"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACbQPdBAAA='}, {'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACazuV4"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACbQPdAAAA='}, {'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACazuVn"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACbQPc-AAA='}, {'@odata.etag': 'W/"CQAAABYAAAB3N5CnvFQSSqeWD3xLEpaUAACazuVg"', 'id': 'AAMkAGYyMzUwZDAxLTJkNmItNGJjYi1iNThkLTcxMWU2MzIxMjg2ZgBGAAAAAAAKfqhNGZ8HSr-8OzkXZKcQBwB3N5CnvFQSSqeWD3xLEpaUAAAAAAEMAAB3N5CnvFQSSqeWD3xLEpaUAACbQPc_AAA='}]
请帮我获取html格式或文本格式的正文。
此行为可能是由权限导致的
Mail.ReadBasic.All
。
拥有 此权限,应用程序无法读取
body
、previewBody
、attachments
和任何扩展属性。
为了能够读取正文,您需要授予更高权限的应用程序权限
Mail.Read
。