C 语言中的 MS Teams 会议集成#

MS Teams Meeting integration in C#

提问人:Yogesh Shinde 提问时间:11/17/2023 更新时间:11/17/2023 访问量:33

问:

我正在尝试在 C# 中创建一个能够与 Microsoft 团队集成的应用程序,我可以在其中安排团队会议、获取会议列表和更新,但在创建 MS Teams 会议时,我收到以下错误。

'代码:BadRequest

消息:/me 请求仅对委托的身份验证流有效。

使用以下代码创建 MS Teams 会议

string clientId = "";
string clientSecret = "";
string tenantId = "";
string redirectUri = ""; 

string authority = $"https://login.microsoftonline.com/{tenantId}";

////string authority = $"https://login.microsoftonline.com/{tenantId}";

// Create a confidential client application
var confidentialClientApp = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithAuthority(new Uri(authority))
    .Build();

// Define the scopes required for Microsoft Graph API
////string[] scopes = { "User.Read", "OnlineMeetings.ReadWrite" }; // Add any additional scopes needed
var scopes = new string[] { "https://graph.microsoft.com/.default" };

try
{
    // Acquire an access token
    var result = await confidentialClientApp
        .AcquireTokenForClient(scopes)
        .ExecuteAsync();

    // Use the access token in your Graph API requests
    string accessToken = result.AccessToken;

    // Create a GraphServiceClient
    var graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
    {
        requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    }));

    // Create an online meeting
    var onlineMeeting = new OnlineMeeting
    {
        StartDateTime = DateTimeOffset.UtcNow.AddMinutes(5), // Schedule the meeting 5 minutes from now
        EndDateTime = DateTimeOffset.UtcNow.AddHours(1), // Set the duration of the meeting (1 hour in this example)
        Subject = "Teams Meeting created via Graph API",
    };

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

    Console.WriteLine($"Meeting created with ID: {createdMeeting.Id}");
    Console.WriteLine($"Join URL: {createdMeeting.JoinWebUrl}");
}
catch (Exception ex)
{
    Console.WriteLine($"Error acquiring token or making Graph API request: {ex.Message}");
}
C# .net-core 集成 、microsoft-graph-teams

评论

0赞 Meghana-MSFT 11/17/2023
根据你收到的错误消息,你似乎正在尝试使用具有应用程序权限的 /me 终结点。/me 终结点旨在使用委托权限,这意味着它在已登录用户的上下文中运行。若要解决此问题,在使用应用程序权限时,应使用 /users/{id | userPrincipalName} 终结点,而不是 /me。此端点允许您指定要为其创建会议的用户。
0赞 Meghana-MSFT 11/17/2023
若要使用此 API 的应用程序权限,租户管理员必须创建应用程序访问策略并将其授予用户,以授权策略中配置的应用程序代表该用户(在请求路径中指定用户 ID)创建联机会议。

答: 暂无答案