如何使用 Microsoft Graph 创建 Microsoft Teams 会议作为应用程序?
How to create a Microsoft Teams Meeting as application using Microsoft Graph?
我正在开发一个日历 Web 应用程序,我想在其中创建 Teams 会议并将会议(join-URL)添加到约会中。
由于并非我的所有用户都通过 Microsoft OAuth 2.0 登录,我必须在服务器端(作为应用程序)创建团队会议。我为我的应用程序创建了一个 Azure 应用程序注册并为其分配了 API-permission“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: '代码: BadRequest
消息:不支持此 API。 createOrGet 操作仅支持 /me/onlineMeetings/createorGet):
var meeting = await client.Communications.OnlineMeetings
.CreateOrGet(new Guid().ToString(), null, end, participants, start, subject)
.PostAsync();
那么还有其他 API 或可能性可以满足我的要求吗?同时,我有点沮丧,非常感谢任何提示。
正如文档所说,添加该策略是必要的,因为应用程序权限不够安全,因为权限非常“大”,而用户权限被微软认为是安全的。
首先,根据我的测试,我发现我可以通过具有正确角色的访问令牌(应用程序 OnlineMeetings.ReadWrite.All*)直接创建在线会议,但我更喜欢使用 createEvent ,因为this API对创建在线会议有更清晰的描述(需要Calendars.ReadWrite的申请权限)。
但是我们还需要注意的是,即使action是由application操作的,但是我们还是需要在request中设置一个user,因为我们需要为会议设置一个organizer,我这里设置了我的user id。我这里没有 运行 任何强大的 shell 脚本。
===============这里有一些示例代码===========
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
}
}
}
我正在开发一个日历 Web 应用程序,我想在其中创建 Teams 会议并将会议(join-URL)添加到约会中。
由于并非我的所有用户都通过 Microsoft OAuth 2.0 登录,我必须在服务器端(作为应用程序)创建团队会议。我为我的应用程序创建了一个 Azure 应用程序注册并为其分配了 API-permission“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: '代码: BadRequest 消息:不支持此 API。 createOrGet 操作仅支持 /me/onlineMeetings/createorGet):
var meeting = await client.Communications.OnlineMeetings
.CreateOrGet(new Guid().ToString(), null, end, participants, start, subject)
.PostAsync();
那么还有其他 API 或可能性可以满足我的要求吗?同时,我有点沮丧,非常感谢任何提示。
正如文档所说,添加该策略是必要的,因为应用程序权限不够安全,因为权限非常“大”,而用户权限被微软认为是安全的。
首先,根据我的测试,我发现我可以通过具有正确角色的访问令牌(应用程序 OnlineMeetings.ReadWrite.All*)直接创建在线会议,但我更喜欢使用 createEvent ,因为this API对创建在线会议有更清晰的描述(需要Calendars.ReadWrite的申请权限)。
但是我们还需要注意的是,即使action是由application操作的,但是我们还是需要在request中设置一个user,因为我们需要为会议设置一个organizer,我这里设置了我的user id。我这里没有 运行 任何强大的 shell 脚本。
===============这里有一些示例代码===========
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
}
}
}