我想使用Azure Graph获取令牌并执行Teams APIC#

问题描述

我通过Azure Graph进行身份验证以获取令牌后,我想使用该令牌创建Teams在线会议。

当我在Teams中创建在线会议时,响应中包含状态码501的错误

如何创建在线会议?

应用流程

  1. 启动应用
  2. 发出GET请求(https:// localhost:43321 / api)
  3. 执行GetAsync方法
  4. 使用GetToken方法获取令牌
  5. 在团队“在线会议”中创建会议
[Route("api")]
[ApiController]
public class ApiController : ControllerBase
{
    [HttpGet]
    public async Task<ContentResult> GetAsync()
    {
        var result = GetToken();
        string accesstoken = result.Result.access_token;

        var httpClient = new WebApp_OpenIDConnect_DotNet.Services.HttpClient();
        var meetingData = new MeetingData
        {
            StartDateTime = new DateTime(2020,8,10,12,57,0),EndDateTime = new DateTime(2020,13,Subject = "meeting"
        };

        string json = JsonConvert.SerializeObject(meetingData,Formatting.Indented);
        (var res,var statusCode) = await httpClient.PostHttpContentWithToken("me/onlineMeetings",accesstoken,json);

        var deserialized = JsonConvert.DeserializeObject<MeetingData>(res);

        return Content(res);
    }
}
private async Task<AccesstokenClass> GetToken()
{
    string tokenUrl = "https://login.microsoftonline.com/tenantId/oauth2/v2.0/token";
    var tokenRequest = new HttpRequestMessage(HttpMethod.Post,tokenUrl);

    tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string,string>
    {
        ["grant_type"] = "client_credentials",["client_id"] = "clientId",["client_secret"] = "clientSecret",["scope"] = "https://graph.microsoft.com/.default"
    });

    var results = new AccesstokenClass();
    var client = new System.Net.Http.HttpClient();

    var tokenResponse = await client.SendAsync(tokenRequest);

    var json = await tokenResponse.Content.ReadAsstringAsync();
    results = JsonConvert.DeserializeObject<AccesstokenClass>(json);

    return results;
}

该应用已在天蓝色广告中注册

GetToken方法中的“ ClientId”,“ tenantId”,“ clientSecret”设置Azure门户概述的信息。

API设置是图像的状态。

enter image description here

解决方法

client_credentials流不支持创建在线会议。

您应使用auth code flow(需要用户交互)或ROPC flow(出于安全原因,Microsoft不推荐)。

请检查。