首页 > 解决方案 > Microsoft Graph OnlineMeeting API 返回状态:NotFound (404) 错误

问题描述

我正在尝试使用以下代码创建在线会议并传递应用注册的所有详细信息。仍然返回 404 错误。


static string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
    
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(LaiAppClientID).WithClientSecret(Secret).WithRedirectUri(redirectURI).Build();
    
AuthorizationCodeProvider authProvider = new AuthorizationCodeProvider(app, scopesssss);
    GraphServiceClient graphClient = new GraphServiceClient(authProvider);
    
graphClient = new GraphServiceClient("https://graph.microsoft.com/beta",
                         new DelegateAuthenticationProvider(
                              async (requestMessage) =>
                              {
                                  var token = await app.AcquireTokenForClient(scopesssss).WithAuthority(String.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenantID), true).ExecuteAsync();
                                  requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
                               
                              }));
    
var onlineMeeting = new OnlineMeeting
                {
                    StartDateTime = DateTimeOffset.Parse("2021-07-12T21:30:34.2444915+00:00"),
                    EndDateTime = DateTimeOffset.Parse("2021-07-12T22:00:34.2464912+00:00"),
                    Subject = "My First MS Teams Meeting",
                    AudioConferencing= audioConferencing
    
                };
    
var task = Task.Run(async () =>
                {
                    return await graphClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting);
                    
                });
var d = task.Result;

在此处输入图像描述

标签: c#azure-active-directorymicrosoft-graph-api

解决方案


如您的代码所示,您使用身份验证代码流获取访问令牌,然后通过 MS Graph API 创建 onlineMeeting。请查看我的代码,它运行良好。

string clientId = "<your-client-id>";
string clientSecret = "<your-client-secret>";
string redirectUri = "<your-redirect-url>";
string authority = "https://login.microsoftonline.com/<tenant>";
string authorizationCode = "<the authorization code>";

string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

IConfidentialClientApplication app = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithRedirectUri(redirectUri)
    .WithClientSecret(clientSecret)
    .WithAuthority(authority)
    .Build();

AuthorizationCodeProvider auth = new AuthorizationCodeProvider(app, scopes);

GraphServiceClient graphServiceClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {

    // Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
    var authResult = await app.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();

    // Add the access token in the Authorization header of the API request.
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
    
})
);

var onlineMeeting = new OnlineMeeting
{
    StartDateTime = DateTimeOffset.Parse("2021-07-12T21:30:34.2444915+00:00"),
    EndDateTime = DateTimeOffset.Parse("2021-07-12T22:00:34.2464912+00:00"),
    Subject = "My First MS Teams Meeting"
};

await graphServiceClient.Me.OnlineMeetings.Request().AddAsync(onlineMeeting);

笔记:

authenticationCode :验证码流程需要先获取此代码,然后才能获取 API 的访问令牌。看到这一步,你可以通过浏览器请求url并用用户登录,然后它会响应代码。

权限:要让它工作,您需要添加权限(您的应用程序 -> API 权限 -> MS Graph ->委托权限-> OnlineMeetings.ReadWrite)来创建在线会议,请参见此处在此处输入图像描述

参考:

关于通过 C# 创建 onlineMeeting 的 Microsoft Graph API:链接

有关使用的示例以及有关代码的AuthorizationCodeProvider更多详细信息。


更新:

消息:仅测试版支持创建具有应用程序权限的在线会议。

API(/v1.0) 只支持委托权限(OnlineMeetings.ReadWrite),不支持应用程序权限。您可以在之前的注释中看到这一点。

在此处输入图像描述

两者/beta也只支持委派权限,见:

在此处输入图像描述


推荐阅读