Google Calendar Api v3 EventDateTime
本文关键字:EventDateTime v3 Api Calendar Google | 更新日期: 2023-09-27 18:10:13
我正在尝试使用谷歌api v3在谷歌日历中插入事件,并在插入期间获得错误。我正在使用c#。
错误:Google.Apis.Requests.RequestError
起始和结束时间无效或不匹配。[400]
Errors [Message]起始和结束时间无效或不匹配。)的位置(-]原因[invalid]域[global]
EventDateTime的代码在这里。
EventDateTime EventStartDTime = new EventDateTime();
EventStartDTime.Date = "2013-06-03";
EventStartDTime.DateTime = "2013-06-03T10:00:00.000+05:00";
EventStartDTime.TimeZone = "Asia/Karachi";
EventDateTime EventEndtDTime = new EventDateTime();
EventEndtDTime.Date = "2013-06-05";
EventEndtDTime.DateTime = "2013-06-05T10:00:00.000+05:00";
EventEndtDTime.TimeZone = "Asia/Karachi";
有谁能帮我解决这个问题吗?
Google calendar V3 API时间戳需要UTC格式,所以你可以提到日期时间和时区(可选)因此,您应该提供以下格式,它将自动获取当前时区:
DateTime start = DateTime.Now;
DateTime end = start + TimeSpan.FromMinutes(30);
var curTZone = TimeZone.CurrentTimeZone;
var dateStart = new DateTimeOffset(start, curTimeZone.GetUtcOffset(start));
var dateEnd = new DateTimeOffset(end, curTimeZone.GetUtcOffset(end));
var startTimeString = dateStart.ToString("o");
var endTimeString = dateEnd.ToString("o");
evnt.Start = new EventDateTime()
{
DateTime = startTimeString
};
evnt.End = new EventDateTime()
{
DateTime = endTimeString
};
在阅读了这里的文档之后,看起来您提供的偏移量是可选的。来自文档:
开始。dateTime | dateTime |时间,日期时间的组合值(根据RFC 3339格式化)。除非在'timeZone'中显式指定了时区,否则需要时区偏移量。
尝试删除DateTime变量中的偏移量或删除TimeZone变量。在我自己使用Python的测试中,这对我来说是有效的。我的代码示例(字典的相关部分):
{
'start': {
'dateTime': '2013-06-05T09:00:00',
'timeZone': 'Europe/Oslo'
},
'end': {
'dateTime': '2013-09-T15:30:00',
'timeZone': 'Europe/Oslo'
},
}
当在某个时间更新(而不是创建)事件时,在darthlukan的回答中指定时区对我不起作用。但是,将DateTimeKind设置为本地时间可以达到这个目的:
DateTime start = DateTime.Now;
DateTime end = start + TimeSpan.FromMinutes(30);
start = DateTime.SpecifyKind(start, DateTimeKind.Local);
end = DateTime.SpecifyKind(end, DateTimeKind.Local);
Event newEvent = new Event();
newEvent.Start = new EventDateTime() { DateTime = start };
newEvent.End = new EventDateTime() { DateTime = end };
这是最后的代码,它现在工作正常:
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "381422642478-tg5s8crg6j1atn259u0aptnltrhmlc24.apps.googleusercontent.com",
ClientSecret = "7yxk_DOKRQv7XNB1rTF5FM2j",
},
new[] { CalendarService.Scope.Calendar },
"user",
CancellationToken.None).Result;
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "Calendar API Sample",
});
DateTime start = DateTime.Now;
DateTime end = start + TimeSpan.FromMinutes(30);
DateTime initiate = DateTime.Now;
DateTime ending = start + TimeSpan.FromMinutes(30);
start = DateTime.SpecifyKind(start, DateTimeKind.Local);
end = DateTime.SpecifyKind(end, DateTimeKind.Local);
var myEvent = new Event
{
Summary = "Google I/O 2015",
Location = "800 Howard St., San Francisco, CA 94103",
Description = "A chance to hear more about Google's developer products.",
Start = new EventDateTime()
{
DateTime = DateTime.Parse("2018-10-12T09:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
End = new EventDateTime()
{
DateTime = DateTime.Parse("2018-10-12T17:00:00-07:00"),
TimeZone = "America/Los_Angeles",
},
Recurrence = new String[] { "RRULE:FREQ=WEEKLY;BYDAY=MO" },
Attendees = new List<EventAttendee>
{
new EventAttendee { Email = "wgcu418@gmail.com"}
},
};
var recurringEvent = service.Events.Insert(myEvent, "primary");
recurringEvent.SendNotifications = true;
recurringEvent.Execute();