谷歌日历API V3 EventDateTime(Google Calendar Api v3 Ev

2019-08-31 18:31发布

我想插入使用谷歌API V3,并在insertion.I正在使用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";

谁能帮我解决这个问题呢?

Answer 1:

谷歌日历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
            };

希望这帮助。



Answer 2:

阅读文档后, 在这里它看起来像偏移你提供是可选的。 从文档:

start.dateTime | 日期时间| 其时,作为组合的日期时间值(根据RFC 3339格式化的)。 时区偏移量是必需的,除非一个时区中“的timeZone”明确指定。

尝试删除您的DateTime变量的偏差或删除时区变量。 在使用Python我自己的测试,这为我工作。 我的代码(词典的相关部分)实施例:

{
    'start': {
        'dateTime': '2013-06-05T09:00:00',
        'timeZone': 'Europe/Oslo'
     },
     'end': {
         'dateTime': '2013-09-T15:30:00',
          'timeZone': 'Europe/Oslo'
      },
}

我希望帮助。



Answer 3:

当更新(而不是创建)在某一时刻的事件,指定的时间段在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 };


Answer 4:

这是最后的代码,现在是工作的罚款:

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


文章来源: Google Calendar Api v3 EventDateTime