Microsoft Graph API - 确定日历事件是否已存在 - C#

Microsoft Graph API - Determine if calendar event already exists - C#

我正在使用 Microsoft Graph API 将日历事件插入到我们的域用户 O365 日历中。我需要确定事件是否存在,但我的研究只展示了如何使用 GraphClient.Me.Events 场景进行搜索。我认为这行不通,因为我们可以全局访问我们域内的所有日历 (Calendars.ReadWrite)。

有没有办法在同步前在适用的域用户日历中搜索事件?

var scopes = new string[] { "https://graph.microsoft.com/.default" };
var confidentialClient = ConfidentialClientApplicationBuilder.Create(clientId).WithTenantId(tenantId).WithClientSecret(clientSecret).Build();
var authResult = await confidentialClient.AcquireTokenForClient(scopes).ExecuteAsync();
            
using (HttpClient c = new HttpClient())
{
    string url = "https://graph.microsoft.com/v1.0/users/" + userEmail + " /calendar/events";
    ToOutlookCalendar createOutlookEvent = CreateEvent();

    HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(createOutlookEvent), Encoding.UTF8, "application/json");
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    request.Content = httpContent;
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);

var response = await c.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();  
}

日历事件目前测试非常简单

public static ToOutlookCalendar CreateEvent()
{
     ToOutlookCalendar outlookEvent = new ToOutlookCalendar
     {
          Subject = "Code test migration appt",
          Body = new Body
          {
              ContentType = "HTML",
              Content = "Testing API with application client authorization"
          },
          Start = new End
          {
               DateTime = "2020-06-22T12:30:00",TimeZone = System.TimeZone.CurrentTimeZone.StandardName
          },
          End = new End
          {
               DateTime = "2020-06-22T14:00:00",TimeZone = System.TimeZone.CurrentTimeZone.StandardName
          },
          Location = new LocationName
          {
               DisplayName = "Sample Location"
          }
     };
     return outlookEvent;
}

假设您针对用户的默认日历,是的。

/me 路径段是 upn 或 userId 的别名,因此类似于:
"https://graph.microsoft.com/v1.0/users/" + userEmail + "/calendar/events?$filter=subject eq '" + knownTitle + "'"
如果您使用的是具有足够权限的应用专用令牌,应该可以正常工作