使用Microsoft图形API C#创建在线会议时未登录AzureActiveDirectory时发生404错误

时间:2020-09-24 09:39:03

标签: c# asp.net azure-active-directory microsoft-graph-api microsoft-teams

我正在尝试使用Microsoft graph API创建Online Meeting,而无需使用asp.net Web应用程序登录到AzureActiveDirectory。为此,我的应用程序具有以下权限,这些权限是根据文档https://docs.microsoft.com/en-us/graph/api/application-post-onlinemeetings?view=graph-rest-1.0&tabs=csharp和客户端凭据身份验证流{{ 3}},而无需与用户立即进行交互。我能够按照client-creds-grant-flow成功地检索访问令牌。 我尝试了Micosoft.Graph和Micosoft.Graph.Beta仍然出现404错误。

创建在线会议代码

  var graphClient = GetAuthenticatedClientCredential();
            var onlineMeeting = new OnlineMeeting
            {
                StartDateTime = DateTimeOffset.Parse("2020-10-01T10:30:34.2444915+00:00"),
                EndDateTime = DateTimeOffset.Parse("2020-10-01T11:00:34.2464912+00:00"),
                Subject = "Create Online Meeting-Without user login to Office 365"
            };

            return await graphClient.Me.OnlineMeetings
                 .Request()
                 .AddAsync(onlineMeeting);

访问令牌代码

 public static async Task<string> GetUserAccessTokenAsyncByCc()
        {
            IConfidentialClientApplication cca = ConfidentialClientApplicationBuilder.Create(appId)
               .WithTenantId(appTenantId)
               .WithClientSecret(appSecret)
               .Build(); 
            
             string[] scopes1 = new string[] { "https://graph.microsoft.com/.default" };
            //string[] scopes1 = new string[] { "https://graph.microsoft.com/OnlineMeetings.ReadWrite.All" };
            // string[] scopes1 = new string[] { "https://graph.microsoft.com/beta/OnlineMeetings.Read.All" };
            //string[] scopes1 = new string[] { "https://graph.microsoft.com/beta/.default" };
            var result = await cca.AcquireTokenForClient(scopes1).ExecuteAsync();
           
            return result.AccessToken;
        }

和身份验证提供者代码

public static GraphServiceClient GetAuthenticatedClientCredential()
        {

            DelegateAuthenticationProvider provider = new DelegateAuthenticationProvider(
                     async (requestMessage) =>
                     {
                         string accessToken = await GetUserAccessTokenAsyncByCc();
                         requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                     });

            GraphServiceClient graphClient = new GraphServiceClient(provider);

            return graphClient;


        }

应用许可图片 https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow

1 个答案:

答案 0 :(得分:1)

您只能使用委派的权限来创建onlineMeeting,因此您必须以用户身份登录,并且不能使用客户端凭据流。您需要使用auth code flow来获取令牌。

enter image description here