Google日历(v3)事件格式

时间:2012-09-28 23:39:55

标签: c# google-calendar-api

我一直在尝试用事件更新日历,但似乎没有通过。你能否评论我是否正确格式化了我的活动?我一直在努力解决这个问题。

private void InsertEventIntoCalendar()
{
    List<EventAttendee> ef = new List<EventAttendee>();
    ef.Add(new EventAttendee{ Email = "event@gmail.com"});

    List<EventReminder> eventReminder = new List<EventReminder>();
    eventReminder.Add(new EventReminder{ Minutes = 4, Method = "email"});

    Event.RemindersData de = new Event.RemindersData();            
    de.Overrides = eventReminder;

    Event newEvent = new Event
    {
        Attendees = ef,
        Reminders  = de,       
        Summary = "Tin Roof",
        Description = "Its gonna be epic",
        Location = "Claremont",                
        Start = new EventDateTime
        {
            Date = "2012-09-30",
            DateTime = "2012-09-30T10:00:00.000-02:00",
            TimeZone = "Cape Town"

        },
        End = new EventDateTime
        {
            Date = "2012-09-30",
            DateTime = new DateTime(   "2012-09-30T10:25:00.000-02:00",
            TimeZone = "Cape Town"

        },
    };

    _service.Events.Insert(newEvent, "event@gmail.com").Fetch();

}

2 个答案:

答案 0 :(得分:2)

以防它帮助其他人。我想出了如何解决这个问题。我也在寻找答案,偶然发现了一个解决方案:

public static void GCalAdd(string summary, string location, string description,
    DateTime start, DateTime end, List<EventAttendee> attendees)
{
    try
    {
        var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
        {
            ClientIdentifier = ClientCredentials.ClientID,
            ClientSecret = ClientCredentials.ClientSecret
        };

        var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);
        var service = new CalendarService(auth);

        var curTimeZone = TimeZone.CurrentTimeZone;
        var dateOffsetStart = new DateTimeOffset(start, curTimeZone.GetUtcOffset(start));
        var dateOffsetEnd = new DateTimeOffset(end, curTimeZone.GetUtcOffset(end));
        var startTimeString = dateOffsetStart.ToString("o");
        var endTimeString = dateOffsetEnd.ToString("o");

        var evt = new Event
        {
            Summary = summary,
            Location = location,
            Description = description,
            Start = new EventDateTime { DateTime = startTimeString },
            End = new EventDateTime { DateTime = endTimeString },
            Attendees = attendees,
        };

        var insert = service.Events.Insert(evt, "primary").Fetch();

        if (insert != null)
        {
            MessageBox.Show(@"calendar item inserted");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return;
    }
}

并且,要使用它,请执行以下操作:

var ea = new EventAttendee
{
    DisplayName = "testname",
    Email = "email@gmail.com",
    Organizer = false,
    Resource = false
};

var ea2 = new EventAttendee
{
    DisplayName = "testname2",
    Email = "email2@gmail.com",
    Organizer = true,
    Resource = false
};

var list = new List<EventAttendee> { ea, ea2 };

var now = DateTime.Now;
var t30 = now + TimeSpan.FromMinutes(30);

Program.GCalAdd("test event", "test location", "test description", now, t30, list);

答案 1 :(得分:1)

我认为提醒数据中的错误。看这里 - Google Calendar API v3 with C# - Unable to insert recurrence exceptions.