从C#服务使用Graph API获取日历计划 [英] Get calendar schedules with Graph API from C# service
问题描述
我需要使用Microsoft Graph API通过.NET Core Windows服务从日历中获取忙/闲计划.根据Microsoft自己的文档,我应该使用以下内容:
I need to use the Microsoft Graph API to get free/busy schedules from the calendar with a .NET Core Windows service. According to Microsoft's own documentation I should use the following:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var schedules = new List<String>()
{
"adelev@contoso.onmicrosoft.com",
"meganb@contoso.onmicrosoft.com"
};
var startTime = new DateTimeTimeZone
{
DateTime = "2019-03-15T09:00:00",
TimeZone = "Pacific Standard Time"
};
var endTime = new DateTimeTimeZone
{
DateTime = "2019-03-15T18:00:00",
TimeZone = "Pacific Standard Time"
};
var availabilityViewInterval = 60;
await graphClient.Me.Calendar
.GetSchedule(schedules,endTime,startTime,availabilityViewInterval)
.Request()
.Header("Prefer","outlook.timezone=\"Pacific Standard Time\"")
.PostAsync();
我已经使用Azure门户注册了一个新应用程序,并为其授予了Calendars.Read.
I have registered a new application using the Azure portal and given it the permission Calendars.Read.
我的C#代码:
try
{
IConfidentialClientApplication clientApplication = ConfidentialClientApplicationBuilder
.Create(_clientId)
.WithTenantId(_tenantId)
.WithClientSecret(_clientSecret)
.Build();
var authProvider = new ClientCredentialProvider(clientApplication);
var graphClient = new GraphServiceClient(authProvider);
var schedules = new List<string>
{
"example@mail.com" // not actual mail used in my application
};
var startTime = new DateTimeTimeZone
{
DateTime = "2020-04-18T00:00:00",
TimeZone = "Europe/Paris"
};
var endTime = new DateTimeTimeZone
{
DateTime = "2020-04-25T23:59:59",
TimeZone = "Europe/Paris"
};
ICalendarGetScheduleCollectionPage scheduleList = await graphClient.Me.Calendar
.GetSchedule(schedules, endTime, startTime, 60)
.Request()
.PostAsync().ConfigureAwait(false);
Console.WriteLine("scheduleList.Count: " + scheduleList.ToList().Count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
运行应用程序时,出现以下异常:
When I run my application I get the following exception:
代码:BadRequest
Code: BadRequest
消息:当前经过身份验证的上下文对此请求无效.当对需要用户登录的端点发出请求时,就会发生这种情况.例如,/me需要登录的用户.代表用户获取令牌以向这些端点发出请求.将OAuth 2.0授权代码流用于移动和本机应用程序,并将OAuth 2.0隐式流用于单页Web应用程序.
Message: Current authenticated context is not valid for this request. This occurs when a request is made to an endpoint that requires user sign-in. For example, /me requires a signed-in user. Acquire a token on behalf of a user to make requests to these endpoints. Use the OAuth 2.0 authorization code flow for mobile and native apps and the OAuth 2.0 implicit flow for single-page web apps.
推荐答案
You are using Client credentials provider to create the authProvider.
但是,客户端凭据仅适用于仅应用程序权限.
However, Client credentials only works for app-only permissions.
但是在您的代码中 graphClient.Me.Calendar
表示您正试图获取我的日历",即已登录用户的日历.
But in your code graphClient.Me.Calendar
means you are trying to get "my calendar", saying that the calendar of the signed-in user.
但是没有登录的用户,因为客户端凭据仅适用于应用程序.
But there is no signed-in user because Client credentials is app-only.
So you need to implement Authorization code provider if you have a signed-in user. Then you can use graphClient.Me.Calendar
to get the calendar.
或者,如果您没有登录用户,则应继续使用客户端凭据提供程序,并将代码修改为: graphClient.Users [用户的objectId"].日历
.
Or if you don't have a signed-in user, you should keep using Client credentials provider and modify the code as: graphClient.Users["objectId of the user"].Calendar
.
这篇关于从C#服务使用Graph API获取日历计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!