2012-09-28 165 views
1

我一直在嘗試用事件更新日曆,但它似乎並未經歷。你能否評論我是否正確地格式化我的活動?我一直在爲此奮鬥一段時間。谷歌日曆(v3)事件格式

private void InsertEventIntoCalendar() 
{ 
    List<EventAttendee> ef = new List<EventAttendee>(); 
    ef.Add(new EventAttendee{ Email = "[email protected]"}); 

    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, "[email protected]").Fetch(); 

} 

回答

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 protected]", 
    Organizer = false, 
    Resource = false 
}; 

var ea2 = new EventAttendee 
{ 
    DisplayName = "testname2", 
    Email = "[email protected]", 
    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);