如何使用 Microsoft Graph 创建 Microsoft Teams 会议作为应用程序?

时间:2021-06-24 08:26:30

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

我正在开发一个日历 Web 应用程序,我想在其中创建 Teams 会议并将会议(加入 URL)添加到约会。

由于并非所有用户都通过 Microsoft OAuth 2.0 登录,我必须在服务器端(作为应用程序)创建团队会议。我为我的应用程序创建了一个 Azure 应用程序注册,并为其分配了 API 权限“OnlineMeetings.ReadWrite.All”(应用程序)。我的应用程序正在使用客户端凭据身份验证流程。

现在的实际问题:

我尝试根据此 Microsoft 文档创建在线会议: Create onlineMeeting

文档说,我必须创建“应用程序访问策略”才能将 onlineMeetings 创建为应用程序 (Allow applications to access online meetings on behalf of a user)

所以我的问题是:真的没有直接的方法来创建在线会议吗?在我看来,这是在 Teams 上下文中使用 Microsoft Graph 时人们想要做的最基本的事情。我无法相信管理员必须花费那么多精力将自己与“Skype for business online”-PowerShell (Configure application access policy) 连接起来并手动授予每个用户的权限。我认为 Azure API 权限应该完全涵盖此类情况。

出于测试目的,我尝试了以下操作,但只得到了 Microsoft.Graph.ServiceException“代码:常规消息:未找到用户。”。我认为这是因为我的 AuthenticationProvider 是一个 ClientCredentialProvider 并且我正在为用户访问 API。

var client = new GraphServiceClient(azureService.ClientCredentialProvider);
        var onlineMeeting = new OnlineMeeting()
        {
            StartDateTime = start,
            EndDateTime = end,
            Subject = subject
        };
     
        var test = await client.Me.OnlineMeetings
            .Request()
            .AddAsync(onlineMeeting);
        

所以,我也是这样试的(Microsoft.Graph.ServiceException: 'Code: BadRequest 消息:发生错误。):

var meeting = await client.Communications.OnlineMeetings                    
                .Request()
                .AddAsync(onlineMeeting);
            

那样 (Microsoft.Graph.ServiceException: 'Code: BadRequest 消息:不支持此 API。 createOrGet 操作仅支持 /me/onlineMeetings/createorGet):

var meeting = await client.Communications.OnlineMeetings
                .CreateOrGet(new Guid().ToString(), null, end, participants, start, subject)
                .PostAsync();

那么是否还有其他 API 或可能性可以满足我的要求?同时,我有点沮丧,如果有任何提示,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

正如文档所说,添加该策略是必要的,因为应用程序权限不够安全,因为权限非常“大”,而用户权限被微软认为是安全的。

首先,根据我的测试,我发现我可以通过具有正确角色的访问令牌(应用程序 OnlineMeetings.ReadWrite.All*)直接创建在线会议,但我更喜欢使用 createEvent相反,因为 this API 对创建在线会议有更清晰的描述(需要 Calendars.ReadWrite 的应用权限)。

但是我们也需要注意,即使action是由应用操作的,但是我们仍然需要在请求中设置一个用户,因为我们需要为会议设置一个组织者,我在这里设置了我的用户ID。我没有在这里运行任何 power shell 脚本。

enter image description here enter image description here enter image description here

==============这里有一些示例代码============

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;

namespace ConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            IConfidentialClientApplication app;
            app = ConfidentialClientApplicationBuilder.Create("client_id_here")
                    .WithClientSecret("client_secret_here")
                    .WithAuthority(new Uri("https://login.microsoftonline.com/your_tenant_name_here"))
                    .Build();

            AuthenticationResult result = null;
            string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
            result = await app.AcquireTokenForClient(scopes)
                    .ExecuteAsync();
            string accesstoken = result.AccessToken;
            Console.WriteLine(accesstoken);
            Console.ReadLine();
            //=======
            //send http post request here
        }
    }
}