简体   繁体   English

Google Calendar Api v3 EventDateTime

[英]Google Calendar Api v3 EventDateTime

I am trying to insert event in google calendar using google api v3 and getting error during insertion.I am using c#. 我试图使用谷歌api v3在谷歌日历中插入事件,并在插入过程中收到错误。我正在使用c#。

Error: 错误:

Google.Apis.Requests.RequestError Google.Apis.Requests.RequestError

Invalid or mismatching start and end times. 开始和结束时间无效或不匹配。 [400] [400]

Errors [Message[Invalid or mismatching start and end times.] Location[ - ] Reason[invalid] Domain[global] 错误[消息[开始和结束时间无效或不匹配。]位置[ - ]原因[无效]域[全局]

My code for EventDateTime is here. 我的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";

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: 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
            };

hope this help. 希望这个帮助。

After reading the docs here it looks like the offset that you're providing is optional. 在阅读了这里的文档之后,看起来您提供的偏移量是可选的。 From the docs: 来自文档:

start.dateTime | start.dateTime | datetime | datetime | The time, as a combined date-time value (formatted according to RFC 3339). 时间,作为组合的日期时间值(根据RFC 3339格式化)。 A time zone offset is required unless a time zone is explicitly specified in 'timeZone'. 除非在'timeZone'中明确指定时区,否则需要时区偏移。

Try removing the offset in your DateTime variable or removing the TimeZone variable. 尝试删除DateTime变量中的偏移量或删除TimeZone变量。 In my own tests using Python this worked for me. 在我自己使用Python的测试中,这对我有用。 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. 在某个时间更新(而不是创建)事件时,在darthlukan的答案中指定时区对我来说不起作用。 However, setting the DateTimeKind to local time did the trick: 但是,将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 };

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM