Microsoft Graph API - Chat Integration
使用OAuth 2.0进行身份验证,需要获取Access Token才能调用API。
Chat.Read - 读取聊天列表Chat.ReadBasic - 读取基本聊天信息ChatMember.Read - 读取聊天成员User.Read - 读取用户信息User.ReadBasic.All - 读取所有用户基本信息curl --location 'https://graph.microsoft.com/v1.0/me/chats?$top=50' \
--header 'Authorization: Bearer {YOUR_ACCESS_TOKEN}' \
--header 'Accept: application/json'
| 参数 | 类型 | 说明 |
|---|---|---|
| $top | int | 返回的聊天数量(默认50,最大50) |
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#chats",
"@odata.count": 9,
"value": [
{
"id": "19:dL5W5f8U1ZDYLzMFpEKrp7PQ8Wr5JIcibVxdGwzdDOc1@thread.v2",
"topic": "PSA Amazon x BF Operation",
"createdDateTime": "2025-06-02T01:46:55.171Z",
"lastUpdatedDateTime": "2025-11-06T07:37:31.968Z",
"chatType": "group",
"tenantId": "9188040d-6c67-4c5b-b112-36a304b66dad"
}
]
}
curl --location 'https://graph.microsoft.com/v1.0/chats/19:CHAT_ID@thread.v2/members' \
--header 'Authorization: Bearer {YOUR_ACCESS_TOKEN}' \
--header 'Accept: application/json'
| 参数 | 类型 | 说明 |
|---|---|---|
| chat-id | string | 聊天的唯一标识符 |
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#members",
"value": [
{
"displayName": "John Smith",
"email": "john@example.com",
"userId": "29133-e297-416e-ace0-67c679949f0d",
"roles": ["owner"],
"visibleHistoryStartDateTime": "2025-06-02T01:46:55.171Z"
}
]
}
curl --location 'https://graph.microsoft.com/v1.0/chats/19:CHAT_ID@thread.v2/messages?$top=50&$orderby=createdDateTime desc' \
--header 'Authorization: Bearer {YOUR_ACCESS_TOKEN}' \
--header 'Accept: application/json'
| 参数 | 类型 | 说明 |
|---|---|---|
| $top | int | 返回的消息数量(1-50) |
| $orderby | string | 排序方式,仅支持:createdDateTime desc |
| $skiptoken | string | 分页token(从@odata.nextLink获取) |
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#chats('...')/messages",
"@odata.count": 10,
"@odata.nextLink": "https://graph.microsoft.com/v1.0/chats/.../messages?$top=10&$skiptoken=...",
"value": [
{
"id": "1762409226255",
"messageType": "message",
"createdDateTime": "2025-11-06T06:07:06.255Z",
"from": {
"user": {
"id": "efe3e583c156b2d6",
"displayName": "BESTFULFILL- Aurora"
}
},
"body": {
"contentType": "html",
"content": "Ok we will source for you
"
},
"attachments": [],
"mentions": [],
"reactions": []
}
]
}
curl --location 'https://login.microsoftonline.com/23dce5d2-92cc-4efa-be2e-6e0ec7306649/oauth2/v2.0/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'client_id=3310a722-f802-4986-9422-43a17795f77d' \
--data-urlencode 'refresh_token={YOUR_REFRESH_TOKEN}'
| 参数 | 类型 | 说明 |
|---|---|---|
| grant_type | string | 固定值:refresh_token |
| client_id | string | 应用程序ID |
| refresh_token | string | 刷新令牌 |
{
"token_type": "Bearer",
"scope": "Chat.Read Chat.ReadBasic ChatMember.Read...",
"expires_in": 4810,
"access_token": "eyJ0eXAiOiJKV1QiLCJub25jZSI6...",
"refresh_token": "1.AcYA0uXcI8yS-k6-Lm4OxzBm..."
}
A: Microsoft Teams的 /messages 端点不支持 $filter 参数。系统会获取所有消息后在客户端按时间范围过滤。
A: Teams API仅支持 $orderby=createdDateTime desc,不支持升序排序。
A: 通常1小时后过期,过期时系统会提示使用Refresh Token刷新。
A: 需要在Azure Portal创建应用程序并配置权限,然后通过OAuth 2.0授权流程获取。