1

我正在尝试使用 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";

谁能帮我解决这个问题?

4

4 回答 4

4

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

希望这有帮助。

于 2013-10-28T09:20:42.480 回答
0

阅读此处的文档后,您提供的偏移量似乎是可选的。从文档:

开始日期时间 | 日期时间 | 时间,作为组合的日期时间值(根据 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'
      },
}

我希望这会有所帮助。

于 2013-05-23T07:47:53.403 回答
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();
于 2018-10-11T11:42:23.080 回答
0

在特定时间更新(而不是创建)事件时,在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 };
于 2016-07-12T22:13:33.740 回答