要从Microsoft Graph API获取对话中仅最新消息的列表,你需要了解以下几个基础概念:
以下是一个使用Microsoft Graph SDK for Python获取对话中最新消息的示例代码:
from msgraphcore import GraphSession
from msgraphcore.middleware.authentication import AccessTokenAuthenticationProvider
import requests
# 设置认证信息
client_id = 'your-client-id'
client_secret = 'your-client-secret'
tenant_id = 'your-tenant-id'
authority_url = f'https://login.microsoftonline.com/{tenant_id}'
resource_url = 'https://graph.microsoft.com'
# 获取访问令牌
token_url = f'{authority_url}/oauth2/v2.0/token'
token_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': f'{resource_url}/.default'
}
token_response = requests.post(token_url, data=token_data)
access_token = token_response.json().get('access_token')
# 创建GraphSession实例
auth_provider = AccessTokenAuthenticationProvider(access_token)
session = GraphSession(auth_provider)
# 获取对话中的最新消息
conversation_id = 'your-conversation-id'
url = f'{resource_url}/v1.0/me/messages?$filter=isRead eq false&$top=1&$expand=conversation'
response = session.get(url)
if response.status_code == 200:
latest_message = response.json().get('value')[0]
print(f'Latest message in conversation {conversation_id}:')
print(f'Subject: {latest_message["subject"]}')
print(f'Body: {latest_message["body"]["content"]}')
else:
print(f'Failed to get latest message. Status code: {response.status_code}')
通过以上步骤和代码示例,你应该能够成功获取对话中的最新消息列表。如果遇到具体问题,可以根据错误信息进行相应的调试和解决。
领取专属 10元无门槛券
手把手带您无忧上云