从Office 365 API获取共享日历

时间:2017-06-07 13:59:26

标签: c# asp.net-mvc azure microsoft-graph office365api

所以,这就是问题所在。我搜遍了MSDN和堆栈,但是没有一个明确的答案如何获取(或者甚至可能在今天?)来访问Office365中的共享日历。

我遵循this教程,这是违规方法:

public async Task<ActionResult> Index()
    {
        List<MyCalendar> myCalendars = new List<MyCalendar>();

        var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
        var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

        AuthenticationContext authContext = new AuthenticationContext(SettingsHelper.Authority, new ADALTokenCache(signInUserId));

        try
        {
            DiscoveryClient discClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri,
                async () =>
                {
                    var authResult = await authContext.AcquireTokenSilentAsync(SettingsHelper.DiscoveryServiceResourceId, new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

                    return authResult.AccessToken;
                });

            var discoveryCapabilitiesResult = await discClient.DiscoverCapabilitiesAsync();

            var dcr = await discClient.DiscoverCapabilityAsync("Calendar");

            OutlookServicesClient exClient = new OutlookServicesClient(dcr.ServiceEndpointUri,
                async () =>
                {
                    var authResult = await authContext.AcquireTokenSilentAsync(dcr.ServiceResourceId, new ClientCredential(SettingsHelper.ClientId, SettingsHelper.AppKey), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

                    return authResult.AccessToken;
                });

            //var calendarsResult = await exClient.Me.Calendars.ExecuteAsync();

            var calendarsResult = await exClient.Me.Calendars.ExecuteAsync();


            do
            {
                var calendars = calendarsResult.CurrentPage;
                foreach (var c in calendars)
                {
                    myCalendars.Add(new MyCalendar { DisplayName = c.Name });
                }

                calendarsResult = await calendarsResult.GetNextPageAsync();

            } while (calendarsResult != null);
        }
        catch (AdalException exception)
        {
            //handle token acquisition failure
            if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
            {
                authContext.TokenCache.Clear();

                //handle token acquisition failure
            }
        }

        return View(myCalendars);
    }

此功能仅返回“我的日历”下的日历,而不返回其他日历(参见图片)

Multiple calendars

我只得到第一个 - 这是我的日历。 “My Calendars”下的第二个与我分享(我不是作者,其他人是我的r / w)和“共享日历”下的一个是公司范围的(在这一个我也有r / w权限)。

有没有办法获得所有这些?在portal.azure.com上添加了我的应用程序,我为我的和日历共享日历设置了权限:

Permissions

我不知道该怎么做。联系人工作正常,但我找不到获取任何共享日历的方法。

1 个答案:

答案 0 :(得分:0)

根据测试,Microsoft Graph可以获取所有日历。以下是其余的供您参考:

从chenchenLi到NanYu的共享日历: enter image description here

查询NanYu的日历:enter image description here

有关用于获取日历的Microsoft Graph REST的更多详细信息,请参阅以下链接:

List calendars