I am trying to insert event in google calendar using google api v3 and getting error during insertion.I am using c#.
Error:
Google.Apis.Requests.RequestError
Invalid or mismatching start and end times. [400]
Errors [Message[Invalid or mismatching start and end times.] Location[
- ] Reason[invalid] Domain[global]
My code for EventDateTime is here.
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";
Can Anyone help me to solve this issue?
Google calendar V3 API timestamp requires UTC format so you can mention datetime and timezone(optional)
so you should provide the below format, which takes current timezone automatically:
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
};
hope this help.
After reading the docs here it looks like the offset that you're providing is optional. From the docs:
start.dateTime | datetime | The time, as a combined date-time value (formatted according to RFC 3339). A time zone offset is required unless a time zone is explicitly specified in 'timeZone'.
Try removing the offset in your DateTime variable or removing the TimeZone variable. In my own tests using Python this worked for me. Example of my code (relevant portion of dictionary):
{
'start': {
'dateTime': '2013-06-05T09:00:00',
'timeZone': 'Europe/Oslo'
},
'end': {
'dateTime': '2013-09-T15:30:00',
'timeZone': 'Europe/Oslo'
},
}
I hope that helps.
When updating (rather than creating) events at a certain time, specifying the time zone as in darthlukan's answer didn't work for me. However, setting the DateTimeKind to local time did the trick:
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 };
This is the final code, It is working fine now:
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();