如果你运行一个普通的样本.

1.GraphClient.Me.FindMeetingTimes,错误[/Me请求仅对委托身份验证流有效.]

2.GraphClient.Users["id"].FindMeetingTimes,出现[无效的用户地址]错误.

以下是结果.

FindMeetingTimes Sample

Graph Explorer

当我在Graph Explorer(https://graph.microsoft.com/v1.0/users/{id}/microsoft.graph.findMeetingTimes)中运行POST时,响应返回正确,但是当我try 使用代码片段中的源代码输出时,在try 使用代码片段中的源代码输出时出现错误.

[示例代码(VSCode)]

Azure函数.Net 6(LTS)独立

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Mvc;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Identity.Client;

namespace Company.Function
{
    public class HttpTrigger0
    {
        private readonly ILogger _logger;

        public HttpTrigger0(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<HttpTrigger5>();
        }

        [Function("HttpTrigger0")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            // Dummy Id
            var userId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";

            // https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=csharp#client-credentials-provider
            var scopes = new[] { "https://graph.microsoft.com/.default" };
            var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
            var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
            var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
            var options = new ClientSecretCredentialOptions  
            {  
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud  
            }; 
            var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret, options);
            var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
            var requestBody = new Microsoft.Graph.Me.FindMeetingTimes.FindMeetingTimesPostRequestBody
            //var requestBody = new Microsoft.Graph.Users.Item.FindMeetingTimes.FindMeetingTimesPostRequestBody
            {
                Attendees = new List<AttendeeBase>
                {
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User1",
                            Address = "user1@test.onmicrosoft.com",
                        },
                    },
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User2",
                            Address = "user2@test.onmicrosoft.com",
                        },
                    },
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User3",
                            Address = "user3@test.onmicrosoft.com",
                        },
                    },
                },
            };
            try {
                var result = await graphClient.Me.FindMeetingTimes.PostAsync(requestBody);
                //var result = await graphClient.Users[userId].FindMeetingTimes.PostAsync(requestBody);
                return new OkObjectResult(result);
            } catch (Exception e) {
                Console.WriteLine("TRC:" + e.StackTrace);
                Console.WriteLine("ERR:" + e.Message);
                return new OkObjectResult(null);
           }
        }
    }
}

推荐答案

当您使用客户端凭据流检索findMeetingTimes(即not supported)时发生错误.

resolve个错误,您需要切换到delegated流,如交互流或授权码流.

在您的应用注册中,使用委派流授予Delegated类型的Calendars.Read.Shared权限.

enter image description here

如果你更喜欢互动流,你需要在Mobile and Desktop applications平台中添加redirect URI,像这样:

enter image description here

在使用交互流的同时,确保您的应用程序注册中有enable public client个流:

enter image description here

在我的例子中,我在控制台应用程序中运行了以下示例C#代码,以使用interactive flow检索findMeetingTimes:

using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Graph.Models.ODataErrors;

// Dummy Id
var userId = "userId";

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenantID";
var clientId = "appID";
var clientSecret = "secret";
var options = new InteractiveBrowserCredentialOptions
{
    TenantId = tenantId,
    ClientId = clientId,
    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    RedirectUri = new Uri("http://localhost"),
};

var interactiveCredential = new InteractiveBrowserCredential(options);

var graphClient = new GraphServiceClient(interactiveCredential, scopes);
var requestBody = new Microsoft.Graph.Me.FindMeetingTimes.FindMeetingTimesPostRequestBody
//var requestBody = new Microsoft.Graph.Users.Item.FindMeetingTimes.FindMeetingTimesPostRequestBody
{
    Attendees = new List<AttendeeBase>
                {
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User1",
                            Address = "user1@test.onmicrosoft.com",
                        },
                    },
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User2",
                            Address = "user2@test.onmicrosoft.com",
                        },
                    },
                    new AttendeeBase
                    {
                        EmailAddress = new EmailAddress
                        {
                            Name = "User3",
                            Address = "user3@test.onmicrosoft.com",
                        },
                    },
                },
};

try
{
    var result = await graphClient.Me.FindMeetingTimes.PostAsync(requestBody);
    Console.WriteLine(result);
}

catch (ODataError odataError)
{
    Console.WriteLine(odataError.Error.Code);
    Console.WriteLine(odataError.Error.Message);
}

Reference:. Choose a Microsoft Graph authentication provider - Microsoft Graph.

Csharp相关问答推荐

Blazor:计算值或保留为默认值

使用C#中的Shape API从Azure目录获取所有用户

使用客户端密钥为Fabric Rest API生成令牌

Unity中的刚体2D运动

当通过Google的Gmail Api发送邮件时,签名会产生dkim = neutral(正文散列未验证)'

在LINQ Where子句中使用新的DateTime

在具有主构造函数的类中初始化属性时出现警告

如何在Windows 11任务调度程序中每1分钟重复一次任务?

附加标题不起作用,而添加则起作用

为具有实体框架后端的Reaction项目 Select 正确的Visual Studio模板

NET8 MAUI并部署到真实设备上进行测试

是否有必要在ASP.NET Core中注册可传递依赖项?

C#按名称从类获取属性值类型<;t>;,我需要反射吗?

当我将`ConcurentDictionary`转换为`IDictionary`时,出现了奇怪的并发行为

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

多个参数的最小API删除

仅在ASP.NETCore应用程序中的附加单独端口上公开一组终结点

通过mini kube中的远程调试Pod与从emoteProcessPickerScript中解析错误输出的代码错误进行比较

如何在JSON:API中定义的&过滤查询参数系列&标准的GET请求中传递多个相关参数?

身份验证中间件如何处理多个方案