2011-04-26 169 views
1

我創建一個新的日曆「測試」,我用這個代碼谷歌日曆API asp.net C#

public static CalendarService GetService(string applicationName, string userName, string password) 
    { 
     CalendarService service = new CalendarService(applicationName); 
     service.setUserCredentials(userName, password); 
     return service; 
    } 
    public static void AddEvent(CalendarService service, string title, string contents, string location, DateTime startTime, DateTime endTime) 
    { 
     Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry(); 

     // Set the title and content of the entry. 
     entry.Title.Text = title; 
     entry.Content.Content = contents; 

     // Set a location for the event. 
     Where eventLocation = new Where(); 
     eventLocation.ValueString = location; 
     entry.Locations.Add(eventLocation); 

     When eventTime = new When(startTime, endTime); 
     entry.Times.Add(eventTime); 

     Uri postUri = new Uri("https://www.google.com/calendar/feeds/test/private/full"); 

     // Send the request and receive the response: 
     AtomEntry insertedEntry = service.Insert(postUri, entry); 
    } 

AddEvent(GetService("regis-test", vUserName, vPassword), "title", "test", "Sibiu", DateTime.Now, DateTime.Now.AddHours(4)); 

有什麼不好?因爲此代碼不會將事件添加到他添加到默認的測試日曆中

回答

1

您需要修改Uri以反映日曆ID。

我建立了我的Gmail帳戶的測試日程,並能夠使用以下URI來我的日曆創建日曆事件:

Uri postUri = new Uri("https://www.google.com/calendar/feeds/[email protected]/private/full"); 

要找到適合你的測試日曆,你需要做的日曆ID如下:

在「我的日曆」下的左側菜單中選擇測試日曆並按設置。

Calendar settings

然後選擇您的日曆:

enter image description here

在下一屏幕,你看到日曆網址下您的日曆ID: calendar-id

使用該ID爲您烏里:

Uri postUri = new Uri("https://www.google.com/calendar/feeds/[email protected]/private/full"); 

以下是通過測試日曆上的C#代碼創建的日曆活動的屏幕截圖: Entry added