发送一个带有访问令牌的 MS Teams 应用程序消息到 MS Teams 中。

huangapple go评论63阅读模式
英文:

Send a chat message into MS Teams as MS Teams app with access token

问题

是可能将聊天消息发送到 MS Teams 中的 1:1 聊天作为已注册应用程序吗?

例如:
每小时触发一个定时任务,并发送一个带有访问令牌的 MS Graph 请求,以将聊天消息发送到具有特定 ID 的聊天中。

英文:

Is it possible to send a chat message into MS Teams 1:1 chat as a registered app?

For example:
Every one hour a cron is triggered and sends a MS Graph request (with an access token) to send a chat message into a chat with a specific id.

答案1

得分: 0

简单回答:是的,这是可能的。

你需要代表用户获取对MS Graph的访问权限,因为一个应用程序本身无法发送聊天消息。

按照这个教程:https://learn.microsoft.com/en-us/graph/auth-v2-user

步骤概要:

1)注册一个应用程序
2)为应用程序分配API权限
3)由你的组织同意这些权限
4)获取用户的授权码(修改并将其粘贴到浏览器中:https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={application_id}&response_type=code&redirect_uri=https://localhost:7193/api/test/authRedirect&response_mode=query&scope=offline_access%20user.read%20chat.read%20chat.ReadWrite&state=12345
5)复制上一个响应中的“code”
6)使用上述教程中的参数调用POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
7)完成!你现在有了调用MS Graph API的访问令牌

我在作用域方面遇到了问题。在教程中还请求了一个“mail.read”作用域。但是我无法获得带有这个作用域的令牌。所以为了测试,我只保留了“user.read”作用域,然后在它起作用后,我尝试了其他附加作用域。

当你更改作用域时,你需要重复第4步来获取新的授权码。

英文:

Simple answer: YES, it's possible.

You need to get access to MS graph on behalf of a user, because an App on it's own cannot send chat messages.

Follow this tutorial: https://learn.microsoft.com/en-us/graph/auth-v2-user

Summary of steps:

  1. Register an App
  2. Give the App Api permissions
  3. Consent with permissions by your organisation
  4. Get user authorization_code (modify and paste this into your browser: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={application_id}&response_type=code&redirect_uri=https://localhost:7193/api/test/authRedirect&response_mode=query&scope=offline_access%20user.read%20chat.read%20chat.ReadWrite&state=12345
  5. copy a "code" from previous response
  6. Call POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token with parameters from tutorial mentioned above
  7. Tadaaa!! You have your access token for calling the MS Graph API

I ran into an issue with scopes. In the tutorial a scope "mail.read" is requested as well. But I was unable to obtain a token with this scope. So for testing purposes I leave only "user.read" scope and after it worked I experimented with additional scopes.

When you change the scope, you need to repeat the step 4. to get a new authorization_code

huangapple
  • 本文由 发表于 2023年3月3日 22:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定