c# - 无法使用 GraphServiceClient 创建具有应用程序身份的在线 session

标签 c# azure microsoft-graph-api microsoft-graph-teams

我使用的是 Azure AD 中的应用程序标识,并且授予了读写权限,我还运行了 Grant-CsApplicationAccessPolicy,以便应用程序标识有权代表 Azure AD 中的真实用户身份创建在线 session

我知道我的设置可以通过图形 API 获取用户。但是,运行以下命令后出现错误:

            var confidentialClient = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithAuthority($"https://login.microsoftonline.com/{tenantId}/v2.0")
                .WithClientSecret(clientSecret)
                .Build();

            GraphServiceClient graphServiceClient =
                new GraphServiceClient("https://graph.microsoft.com/beta", new DelegateAuthenticationProvider(async (requestMessage) =>
                {

                        var authResult = await confidentialClient
                            .AcquireTokenForClient(scopes)
                            .ExecuteAsync();

                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
                })
                    );

                var onlineMeeting = new OnlineMeeting
                {
                    StartDateTime = DateTimeOffset.Parse("2020-12-25T21:30:34.2444915+00:00"),
                    EndDateTime = DateTimeOffset.Parse("2020-12-25T22:00:34.2464912+00:00"),
                    Subject = "User Token Meeting 1"
                    
                };

                var meetingInstance = await graphServiceClient.Me.OnlineMeetings
                    .Request()
                    .AddAsync(onlineMeeting);

错误信息如下,为什么会显示User Look up by user id failed in AAD

状态:未找到 (404) 操作ID:8d06ff01-1dc3-49d1-9ced-9db6a919b162

客户端相关 ID:53b4478e-ba86-48ca-bb5b-25e5ef50c187

服务器错误:AAD 中按用户 ID 进行用户查找失败。

客户端异常:HTTP 请求的处理导致异常。有关详细信息,请参阅此异常的“Response”属性返回的 HTTP 响应。

内部错误:

其他数据:

日期:2020-12-16T21:08:31

请求 ID:d60858cf-5ef5-4a0d-8d67-181f80ed6c35

客户端请求 ID:d60858cf-5ef5-4a0d-8d67-181f80ed6c35

客户端请求 ID:d60858cf-5ef5-4a0d-8d67-181f80ed6c35

在 Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage 请求、HttpCompletionOption 完成选项、CancellationToken 取消 token ) 在 Microsoft.Graph.BaseRequest.SendRequestAsync(对象可序列化对象、CancellationToken 取消 token 、HttpCompletionOption 完成选项) 在 Microsoft.Graph.BaseRequest.SendAsync[T](对象可序列化对象,CancellationToken 取消 token ,HttpCompletionOption 完成选项) 在 D:\VSTS\msteam\MSTeam\MSTeam\Program.cs 中的 MSTeam.Program.Main(String[] args):第 62 行

最佳答案

开发人员是正确的。

基于此document :

Request when using an application token: POST /users/{userId}/onlineMeetings.

因此,您应该在此处使用 graphServiceClient.Users["{userId}"].OnlineMeetings 而不是 graphServiceClient.Me.OnlineMeetings

userId 是用户的对象 ID。当你 Configure application access policy ,您需要将策略授予用户:

Grant-CsApplicationAccessPolicy -PolicyName Test-policy -Identity "ddb80e06-92f3-4978-bc22-a0eee85e6a9e"

ddb80e06-92f3-4978-bc22-a0eee85e6a9e 正是 userId

我的代码供您引用:

        // Configure the MSAL client as a confidential client
        var confidentialClient = ConfidentialClientApplicationBuilder
            .Create("{client_id}")
            .WithTenantId("{tenant_id}")
            .WithClientSecret("{client_secret}")
            .Build();

        ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClient);

        GraphServiceClient graphServiceClient = new GraphServiceClient(authProvider);

        var onlineMeeting = new OnlineMeeting
        {
            StartDateTime = DateTimeOffset.Parse("2021-01-12T21:30:34.2444915+00:00"),
            EndDateTime = DateTimeOffset.Parse("2021-01-12T22:00:34.2464912+00:00"),
            Subject = "User Token Meeting123"
        };

        var meeting = await graphServiceClient.Users["{userId}"].OnlineMeetings
            .Request()
            .AddAsync(onlineMeeting);

关于c# - 无法使用 GraphServiceClient 创建具有应用程序身份的在线 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65405991/

相关文章:

asp.net-mvc - ApplicationUser - 我如何使用它?我需要在 using 子句中添加什么?

outlook - 使用 MS Microsoft Graph 按类别获取和过滤 Outlook 事件

microsoft-graph-api - 为 OneDrive Business 创建订阅返回 403 禁止

javascript - 正在访问 MS Graph 'Unable to retrieve user' s mysite URL。'使用 One Drive 客户端凭据

c# - 根据值对 3 个列表进行排序

azure - 我可以使用 Azure Oauth 应用程序的 client_id 和 client_secret 获取用户配置文件吗?

c# - 如何以编程方式调用设置魅力?

Azure 数据工厂 - Azure SQL 数据库作为 Power BI 的 JSON API

c# - 在 C# 中快速读取 100+ GB 文件中的行的最快方法

c# - 值等于和循环引用 : how to resolve infinite recursion?