简体   繁体   English

使用 Microsoft.Graph 获取共享日历事件

[英]Getting shared calendar events with Microsoft.Graph

I'm trying to recreate the following example: https://learn.microsoft.com/en-us/graph/outlook-get-shared-events-calendars我正在尝试重新创建以下示例: https://learn.microsoft.com/en-us/graph/outlook-get-shared-events-calendars

I'm using ASP.NET Core 3.1, Azure AD, and the Microsoft.Graph API. I was able to retrieve events from my personal calendar, as well as the name, ID, owner etc. from the shared calendars.我正在使用 ASP.NET Core 3.1、Azure AD 和 Microsoft.Graph API。我能够从我的个人日历中检索事件,以及从共享日历中检索名称、ID、所有者等。 The events from the shared calendars, however, refuse to show.但是,共享日历中的事件拒绝显示。 I couldn't find any examples, Microsoft's or otherwise.我找不到任何例子,无论是微软的还是其他的。

Here's the code I'm using:这是我正在使用的代码:

[Authorize]
[AuthorizeForScopes(ScopeKeySection = "MicrosoftGraph:Scopes")]
public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;
    private readonly GraphServiceClient graphServiceClient;
    private readonly MicrosoftIdentityConsentAndConditionalAccessHandler _consentHandler;

    private string[] _graphScopes;

    public HomeController(ILogger<HomeController> logger, GraphServiceClient gsc,
                        MicrosoftIdentityConsentAndConditionalAccessHandler consentHandler
        )
    {
        _logger = logger;
        graphServiceClient = gsc;

        this._consentHandler = consentHandler;

        _graphScopes = Startup.ConfigurationInstance.GetSection("MicrosoftGraph")["Scopes"].Split(" ");
    }

    [AuthorizeForScopes(ScopeKeySection = "MicrosoftGraph:Scopes")]
    public IActionResult Index()
    {
        User currentUser = null;

        try
        {
            currentUser = graphServiceClient.Me.Request().GetAsync().Result;
        }
        // Catch CAE exception from Graph SDK
        catch (ServiceException svcex) when (svcex.Message.Contains("Continuous access evaluation resulted in claims challenge"))
        {
            try
            {
                Console.WriteLine($"{svcex}");
                string claimChallenge = WwwAuthenticateParameters.GetClaimChallengeFromResponseHeaders(svcex.ResponseHeaders);
                _consentHandler.ChallengeUser(_graphScopes, claimChallenge);
                return new EmptyResult();
            }
            catch (Exception ex2)
            {
                _consentHandler.HandleException(ex2);
            }                
        }

        var viewOptions = new List<QueryOption>
            {
                new QueryOption("startDateTime", DateTime.Today.ToString("o")),
                new QueryOption("endDateTime", DateTime.Today.AddDays(20).ToString("o"))
            };

        var sha = graphServiceClient.Me.Calendars
                                       .Request()
                                       .Select(e => e.Id)
                                       .GetAsync().Result.ToList();

        var x = new List<Event>();

        foreach(var row in sha)
        {
            var id = row.Id;
            var data = graphServiceClient.Me.Calendars[id].Request(viewOptions)
                .Select(e => e.Events).GetAsync().Result;
            ICalendarEventsCollectionPage d1 = data != null? data.Events: null;

            if (d1 != null)
            {
                x.AddRange(d1.ToList());
            }
        }
    }
}

The above retrieves null events.以上检索了 null 个事件。

I tried to access the calendar directly from another user, as per another one of Microsoft's examples:根据 Microsoft 的另一个示例,我尝试直接从另一个用户访问日历:

var x = graphServiceClient.Users["<email>"]
        .CalendarView
        .Request(viewOptions)
        .Select(e => new
        {
            e.Subject,
            e.Organizer,
            e.Start,
            e.End
        }).GetAsync().Result;

The only thing I get from this code is 'access denied'.我从这段代码中得到的唯一信息是“访问被拒绝”。

My appsettings.json with the scopes set in the Azure AD tenant:我的appsettings.json具有在 Azure AD 租户中设置的范围:

"AzureAd": {
    "Instance": "https://login.microsoftonline.com/",
    "Domain": "<domain>",
    "TenantId": "<id>",
    "ClientId": "<id>",
    "CallbackPath": "/signin-oidc",
    "SignedOutCallbackPath": "/signout-callback-oidc",
    "ClientSecret": "<secret>"
  },
  "AllowedHosts": "*",
  "MicrosoftGraph": {
    "Scopes": "User.Read MailboxSettings.ReadWrite Calendars.ReadWrite.Shared",
    "BaseUrl": "https://graph.microsoft.com/v1.0"
  }

Any help with this would be greatly appreciated.对此的任何帮助将不胜感激。

Your code and configuration don't seem to be the problem.您的代码和配置似乎不是问题所在。 Please check whether the Permissions to allow access is enabled in Outlook's calendar.请检查 Outlook 日历中是否启用了允许访问的权限。

在此处输入图像描述

If the problem persists, here is a detailed documentation to guide you through the configuration step by step.如果问题仍然存在,这里有一个详细的文档来指导您逐步完成配置。 You can refer to this documentation to see if some steps are missing.您可以参考此文档以查看是否缺少某些步骤。

Hop this can help you.希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GraphServiceClient 中的某些方法不可用 Microsoft.Graph - Some methods in GraphServiceClient not available Microsoft.Graph 如何使用 Microsoft graph api 将 colors 应用于 outlook 日历事件? - How to apply colors to outlook calendar events using Microsoft graph api? Microsoft.Graph SDK 获取“组”类型组的成员 - Microsoft.Graph SDK Get members of the group of type 'Group' 当我已经将用户的刷新令牌存储在数据库中时,使用 Microsoft.Graph SDK 的最佳方式是什么? - Best way to use Microsoft.Graph SDK when I already have users' refresh tokens stored in database? Microsoft Graph API - 管理员同意更新用户日历 - Microsoft Graph API - Administrator Consent to update User Calendar Microsoft Graph 筛选器出现“不受支持的查询”错误 - Microsoft Graph filter is getting an error of Unsupported Query 从asp.net核心身份验证Web应用程序获取Microsoft Graph的访问令牌 - Getting Access Token for Microsoft Graph from asp.net core Authentication web app Microsoft Graph 访问令牌刷新 - Microsoft Graph access token refresh 如何在范围中包含 Microsoft 图形? - How to include Microsoft graph in scopes? 如何在 Microsoft Graph 中使用 DI - How to use DI with Microsoft Graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM