[英]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.