我一直在尝试在应用程序模式下向单个用户发送聊天消息,我的意思是,不需要每个用户都进行身份验证并授予对应用程序的访问权限。这个想法是让它对他们透明。
所以我发送此请求以获取访问令牌:
POST https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token
grant_type=client_credentials,
client_id={app id registered in azure portal},
client_secret={registered app key},
scope=https://graph.microsoft.com/.default
发送此请求,我得到了令牌。
使用此令牌,我尝试使用此请求向用户发送消息:
POST https://graph.microsoft.com/beta/chats/{chat-id}/messages
使用 JSON 格式的有效负载:
{
"body": {
"content": "Hello World"
}
}
问题是我收到此错误消息:
{
"error": {
"code": "Unauthorized",
"message": "Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details.",
"innerError": {
"date": "2023-05-18T17:49:24",
"request-id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"client-request-id": "xxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
有趣的一点是,我可以使用所解释的相同逻辑创建聊天。 我用我得到的令牌发送这个请求:
https://graph.microsoft.com/v1.0/chats
Json 格式的负载:
{
"chatType": "oneOnOne",
"members": [
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"[email protected]": "https://graph.microsoft.com/beta/users('user1')"
},
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"[email protected]": "https://graph.microsoft.com/beta/users('user2')"
}
]
}
创建聊天时工作正常,但发送消息时出现错误。
我拥有所有应用程序权限。
根据此MS 文档,不支持使用应用程序权限发送聊天消息。由于客户端凭据流程仅适用于Application权限,因此无法使用该流程发送聊天消息。
但是对于通过 Microsoft Graph API 创建聊天,支持应用程序权限。检查此MS 文档。
我注册了一个 Azure AD 应用程序并授予了 API 权限,如下所示:
现在,我通过 Postman 使用客户端凭证流生成访问令牌,如下所示:
POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
grant_type:client_credentials
client_id: <appID>
client_secret:<secret>
scope: https://graph.microsoft.com/.default
回复:
当我使用上面的令牌通过此查询创建聊天时,我成功地得到了response,如下所示:
POST https://graph.microsoft.com/v1.0/chats
Content-Type: application/json
{
"chatType": "oneOnOne",
"members": [
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"[email protected]": "https://graph.microsoft.com/beta/users('user1')"
},
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"[email protected]": "https://graph.microsoft.com/beta/users('user2')"
}
]
}
回复:
但是,当我尝试使用相同的令牌在此聊天中发送消息时,我遇到了同样的错误:
POST https://graph.microsoft.com/beta/chats/{chat-id}/messages
{
"body": {
"content": "Hello World"
}
}
回复:
要解决该错误,您需要向应用程序授予委托权限,并使用委托流程(如授权代码流程或用户名密码流程等)生成访问令牌...
我在我的应用程序中添加了
ChatMessage.Send
委托权限,如下所示:
就我而言,我使用用户名密码流程生成了访问令牌,不需要用户交互,如下所示:
POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
grant_type:password
client_id: <appID>
client_secret:<secret>
scope: https://graph.microsoft.com/.default
username: [email protected]
password: xxxxxxxxxx
回复:
当我在下面的查询中使用此令牌在聊天中发送消息时,我成功地得到了response,如下所示:
POST https://graph.microsoft.com/beta/chats/{chat-id}/messages
{
"body": {
"content": "Hello World"
}
}
回复:
如何在abap sap中使用此流程我创建了一个oauth 2.0客户端的配置文件,其中包含所有必要的字段,例如client_id、客户端密钥、令牌端点,但它还有另外2个字段,我不知道如何将它们添加到我的配置文件中这是用户名和密码