我正在尝试使用google api v3在Google日历中插入事件并在插入期间收到错误。我正在使用c#。
错误:
Google.Apis.Requests.RequestError
开始和结束时间无效或不匹配。 [400]
错误[消息[开始和结束时间无效或不匹配。]位置[ - ]原因[无效]域[全局]
我的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";
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:3)
Google日历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
};
希望这有帮助。
答案 1 :(得分:0)
阅读文档here之后,您提供的偏移量似乎是可选的。来自文档:
start.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'
},
}
我希望有所帮助。
答案 2 :(得分:0)
在特定时间更新(而不是创建)事件时,在darthlukan's answer中指定时区并不适用于我。但是,将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 };
答案 3 :(得分:0)
这是最终代码,现在可以正常工作:
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();