1

我一直在尝试使用事件更新日历,但似乎没有通过。你能评论一下我是否正确地格式化了我的活动吗?我已经为此苦苦挣扎了一段时间。

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

}
4

2 回答 2

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);
于 2013-02-13T20:03:28.727 回答
1

我认为提醒数据有误。看这里 -带有 C# 的 Google Calendar API v3 - 无法插入重复异常。

于 2013-01-28T15:34:35.967 回答