Chr*_*ang 2 c# .net-core azure-ad-graph-api microsoft-graph-api
我需要使用 Microsoft Graph API 通过 .NET Core Windows 服务从日历中获取忙/闲时间表。根据微软自己的文档,我应该使用以下内容:
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();
Run Code Online (Sandbox Code Playgroud)
我已经使用 Azure 门户注册了一个新应用程序,并为其授予了 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);
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的应用程序时,出现以下异常:
代码:BadRequest
消息:当前经过身份验证的上下文对此请求无效。当向需要用户登录的端点发出请求时,就会发生这种情况。例如,/me 需要登录用户。代表用户获取令牌以向这些端点发出请求。将 OAuth 2.0 授权代码流用于移动和本机应用程序,将 OAuth 2.0 隐式流用于单页 Web 应用程序。
| 归档时间: |
|
| 查看次数: |
588 次 |
| 最近记录: |