0

我正在尝试使用 o365 event rest api 为全天创建事件,但出现错误的开始和结束时间应该是午夜(dd/MM/yyyy) 结束日期: 02/01/2016 12:00 AM (dd/MM/yyyy) 正如 api 所说,全天活动应该有 24 小时的间隔,我以同样的方式做了同样的事情,仍然是它的抛出错误。

我尝试了不同的情况来创建事件,但是我传递给rest api的日期之间存在差异,我也尝试过传递时区,但仍然存在差异。

使用 API 2.0 得到不同的问题。发现了不兼容的类型。发现类型“Microsoft.OutlookServices.DateTimeTimeZone”属于“复杂”类型,而不是预期的“原始”类型。

var startDt=new DateTime(2016, 1, 22, 00, 00, 0);
startDate.DateTime = startDt.ToString(dateTimeFormat);
startDate.TimeZone = timeZone;

DateTimeTimeZone endDate = new DateTimeTimeZone();
endDate.DateTime = startDt.AddDays(1).ToString(dateTimeFormat);
endDate.TimeZone = timeZone;

Event newEvent = new Event
{
Subject = "Test Event",
Location = location,
Start = startDate,
End = endDate,
Body = body
};

            try
            {
                // Make sure we have a reference to the Outlook Services client
                var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");

                // This results in a call to the service.
                await outlookServicesClient.Me.Events.AddEventAsync(newEvent);
                await ((IEventFetcher)newEvent).ExecuteAsync();
                newEventId = newEvent.Id;
            }
            catch (Exception e)
            {
                throw new Exception("We could not create your calendar event: " + e.Message);
            }
            return newEventId;
4

1 回答 1

0

为了使用 v2 API,您需要来自 NuGet 的v2 库(看起来您正在这样做)和 v2 端点(由于错误您不是)。v2 库与 v1 端点不兼容,这导致了您看到的错误。

在 v1 端点中,Start只是End简单的 ISO 8601 日期/时间字符串。在 v2 中,它们是复杂类型。

在 v2 中,您需要使用时区指定开始和结束日期/时间,还需要将IsAllDay事件的属性设置为true.

OutlookServicesClient client = new OutlookServicesClient(
  new Uri("https://outlook.office.com/api/v2.0"), GetToken);

Event newEvent = new Event()
{
  Start = new DateTimeTimeZone() 
  { 
    DateTime = "2016-04-16T00:00:00", 
    TimeZone = "Pacific Standard Time" 
  },
  End = new DateTimeTimeZone() 
  { 
    DateTime = "2016-04-17T00:00:00", 
    TimeZone = "Pacific Standard Time" 
  },
  Subject = "All Day",
  IsAllDay = true
};

await client.Me.Events.AddEventAsync(newEvent);

要在 v1 中执行此操作,您需要计算适当的偏移量并将它们包含在 ISO 8601 字符串中(补偿 DST)。因此,由于我们在 DST,太平洋目前是 UTC-7(而不是标准的 -8)。您还需要设置StartTimeZoneEndTimeZone属性。所以我会做类似的事情:

Event newEvent = new Event()
{
  Start = "2016-04-16T00:00:00-07:00", 
  End = "2016-04-17T00:00:00-07:00",
  StartTimeZone = "Pacific Standard Time",
  EndTimeZone = "Pacific Standard Time", 
  Subject = "All Day",
  IsAllDay = true
};
于 2016-04-15T12:52:45.600 回答