在团队自定义应用程序上使用没有用户(应用程序身份)的Microsoft Graph API

发布于 2025-02-09 03:45:21 字数 1298 浏览 3 评论 0 原文

我一直在使用TeamSFX SDK开发团队自定义应用程序。 我想使用应用程序身份使用Microsoft Graph API。 因此,我提到了Microsoft官方文档,但是我无法实现自己想做的事情。

- 引用文档:。 在“创建MicrosoftGraplClient Service”下,“ Indoke Graph API没有用户(应用程序身份)”部分。

我尝试了以下操作:

  1. 我从“ SSO启用”选项卡中创建了一个新的Teams应用程序,并在Visual Studio Code上使用Teams Toolkit创建了一个新团队应用程序。
  2. 我在下面编辑了一个Graph.jsx,以获取用户信息。
import { createMicrosoftGraphClient, IdentityType, TeamsFx } from "@microsoft/teamsfx";
useEffect(() => {
    const getProfile = async () => {
        const teamsfx = new TeamsFx(IdentityType.App);
        const graphClient = createMicrosoftGraphClient(teamsfx);
        const profile = await graphClient.api("/users/[email protected]").get();
        return profile;
    };
    const profile = getProfile();
}, []);
  1. 我通过在Visual Studio代码中击中F5密钥来调试项目。

尽管我尝试了该文档所说的话,但控制台日志表示“ TeamSFX中不支持应用程序身份”。

我应该如何编辑我的projec以使用Microsoft Graph API,而无需用户身份(即使用应用程序身份)?

I've been developing a Teams Custom App with the TeamsFx SDK.
I want to use the Microsoft Graph API using an Application identity.
So, I referred to the Microsoft official documentation, however I wasn't able to achieve what I wanted to do.

 - Referred document: https://learn.microsoft.com/en-us/microsoftteams/platform/toolkit/teamsfx-sdk.
Under "Create MicrosoftGraphClient service", "Invoke Graph API without user (Application Identity)" section.

I tried the following:

  1. I created a new Teams app from "SSO-enabled tab" sample with Teams Toolkit on Visual Studio Code.
  2. I edited a Graph.jsx as below to get a user info.
import { createMicrosoftGraphClient, IdentityType, TeamsFx } from "@microsoft/teamsfx";
useEffect(() => {
    const getProfile = async () => {
        const teamsfx = new TeamsFx(IdentityType.App);
        const graphClient = createMicrosoftGraphClient(teamsfx);
        const profile = await graphClient.api("/users/[email protected]").get();
        return profile;
    };
    const profile = getProfile();
}, []);
  1. I debugged the project by hitting the F5 key in Visual Studio Code.

Although I tried what the document said, the console log said "Application identity is not supported in TeamsFx".

How should do I edit my projec to use Microsoft Graph API without a user identity (i.e. using Application Identity)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮色兮凉城 2025-02-16 03:45:21

浏览器(选项卡页面)中不支持应用程序身份,因此您需要服务器环境来使用它。

您可以添加一个Azure功能,并在其中使用应用程序身份来实现所需的效果。这是Teams Toolkit v4.0.1中的步骤:

  1. 从“启用SSO”选项卡”模板中创建新项目。
  2. 选择默认名称的“添加功能”中的“ Azure函数”。
  3. 修改“ API/GetUserProfile/index.ts”中的代码。
teamsfx = new TeamsFx(IdentityType.App);
...
const graphClient: Client = createMicrosoftGraphClient(teamsfx, [".default"]);
const profile: any = await graphClient.api("/users/[email protected]").get();
res.body.graphClientMessage = profile;
  1. 在AAD门户上配置“ user.read.all”权限和授予管理员同意。
  2. 在选项卡页面上运行“ F5”,然后单击“调用Azure函数”,将调用Azure函数,并使用应用程序身份获取图形数据。您应该在下面查看用户信息。

Application Identity is not supported in browser (Tab page), so you need a server environment to use it.

You could add an Azure Function and use the Application Identity in it to achieve desired effect. Here's the steps in Teams Toolkit v4.0.1:

  1. Create new project from "SSO-enabled tab" template.
  2. Choose "Azure Functions" in "Add features" with default name.
  3. Modify the code in "api/getUserProfile/index.ts".
teamsfx = new TeamsFx(IdentityType.App);
...
const graphClient: Client = createMicrosoftGraphClient(teamsfx, [".default"]);
const profile: any = await graphClient.api("/users/[email protected]").get();
res.body.graphClientMessage = profile;
  1. Configure "User.Read.All" permission and grant admin consent on AAD portal.
  2. Run "F5" and click "Call Azure Function" on tab page, the Azure Function will be invoked and get Graph data using Application Identity. You should see the user information below.
浮萍、无处依 2025-02-16 03:45:21

您要做的是不安全的,因为您的代码作为应用程序级别的安全性运行,但在客户端端运行。具有这种特权的代码只能在服务器端或在受保护的API后面运行(例如,它使用SSO验证用户的安全性以确保用户有效,然后在服务器上执行)。

该视频对它应该如何工作给出了一些想法: https:// https://www.youtube 。

​然后打电话)。您可以跳过该部分,只使用应用程序令牌,但是如我所提到的那样,您应该在服务器中而不是在客户端上发生这种情况。

What you're trying to do is insecure, because you have code running as Application level security, but running on the client side. Code running with that kind of privilege should only be running on the server side, or in this case behind a protected API (e.g. one that it validating the user's security using SSO to ensure the user is valid, and then executing on the server).

This video gives a bit of an idea of how it should be working: https://www.youtube.com/watch?v=kruUnaZgQaY

In the video, they're actually doing a token -exchange- (exchanging from the pure client side SSO token, and getting an "on behalf of" token in the backend, and making the call then). You can skip that part and just use the application token, but you should, as I mentioned, have this occurring in the server, not on the client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文