如何使用 Bot Builder 向第三方服务发出经过身份验证的请求?

How can I use Bot Builder to make an authenticated request to a third party service?

我正在创建一个 Microsoft Teams 机器人,它将允许用户使用第三方 OAuth 登录并向非 Microsoft Web 服务发出经过身份验证的请求。我可以使用 OAuthPrompt 通过我的第三方 OAuth 提供商登录,使用在 Azure 机器人服务中设置的连接。当我进行身份验证时,我收到一个 guid,它可能是与我在 Bot Framework 令牌服务中的 OAuth 令牌相对应的密钥。

我不清楚如何使用此 guid 对我的网络服务发出经过身份验证的请求。 documentation 状态(强调我的):

To perform certain operations on behalf of a user ... the bot will need to call an external service, such as the Microsoft Graph, GitHub, or a company's REST service. Each external services has a way of securing those calls, and a common way to secure such a call is to issue those requests using a user token that uniquely identifies the user on that external service (sometimes referred to as a JWT).

此文档向我表明应该可以通过 Bot 令牌框架服务向我的 Web 服务发出经过身份验证的请求,或者应该可以检索我的 OAuth 令牌以直接向我的服务发出经过身份验证的请求我的机器人。我找不到任何人这样做的例子,并且花了几天时间查看文档和 botbuilder 源存储库。

机器人应该使用网络服务的结果来响应用户的消息或操作。例如,如果用户将 link 粘贴到除非登录才可用的特定网页,机器人将使用用户的 OAuth 身份创建该页面的 link 预览并将其发送到对话。

我正在使用 Bot Builder v4 和 Bot Framework Emulator。

你绝对可以做到这一点。我会为您指明要查看的正确文档。

首先,您需要使用 Teams OAuth Sample. I've got a PR 添加说明以使其正常工作,但其要点是:

  1. Add Authentication to your Bot
  2. 将您的 OAuth 连接名称添加到 appsettings.json 中的 ConenctionName 键(连同 appid/password)
  3. Enable the Teams Channel
  4. Install your Bot to Teams via App Studio, ensuring that you've added token.botframework.com to the Valid Domains(注意:在 App Studio 中,这是 Manifest Editor > Your App > Domains and Permissions

一旦通过身份验证,它将 return 一个令牌并为每条消息验证它。如果你有一个特定的 OAuth 提供程序未在 Azure 门户的 OAuth 设置边栏选项卡中列出,并且他们有免费试用版,请告诉我,我可以帮助你测试它。关键是在您输入设置后,您保存并单击 "Test" 按钮以确保它在将其添加到您的机器人之前在 Azure 门户中工作。