如何使用. NET 创建Google日历事件

分享于 

8分钟阅读

Web开发

  繁體 雙語

介绍

本文将向你提供有关如何在. NET 中操作Google日历事件的信息。 我只执行了两种功能: 日历检索,然后根据选定的日历填充事件。 最后,为选定的Google日历创建事件。

使用代码

你必须添加对以下dll的引用:

  • Google.Google.GData.AccessControl.dll
  • Google.GData.Calendar.dll
  • Google.GData.Client.dll
  • Google.GData.Extensions.dll

首先,我们将获得用户 Having 所拥有的所有日历:Google日历。 为此,我们将创建一个 CalendarService 对象,如下所示。 在这里,UserNamePassword 将是你的。 然后我们将创建一个 CalendarQuery 对象,并使用 CalendarService 对象来根据给定的查询获取日历。

现在,为了根据所选日历获取事件,首先,我们必须为特定的日历创建 CalendarURI。 然后,我们将创建一个 RequestFactory 来创建特定查询(。选择,更新,删除 等等 ) 和代理对象的请求。 最后,我们将向 CalendarService 对象提供最终查询,以从所选日历获取 EventFeed array ( 事件)。 在Google日历中创建事件的功能非常相似,除了在查询( 这次,我们将请求插入,并为事件。比如。名称。描述。位置 等等 设置不同的值) 中将有更改的事实。

CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar" + 
 "/feeds/default/owncalendars/full");
CalendarFeed resultFeed = myService.Query(query);if (cmbGoogleCalendar.SelectedIndex >= 0)
{
 //Get the selected calendar whose events you want to retrievethis.CalendarURI.Text = "http://www.google.com/calendar/feeds/" + 
 ((CalendarEntry)(cmbGoogleCalendar.SelectedItem)).SelfUri.ToString(
 ).Substring(((CalendarEntry)
 (cmbGoogleCalendar.SelectedItem)).SelfUri.ToString().LastIndexOf("/") 
 + 1) + "/private/full"; 
}string calendarURI = this.CalendarURI.Text;
userName = this.UserName.Text;
passWord = this.Password.Text;this.entryList = new ArrayList(50); 
ArrayList dates = new ArrayList(50); 
EventQuery query = new EventQuery();
GDataGAuthRequestFactory requestFactory = 
 (GDataGAuthRequestFactory)service.RequestFactory;
IWebProxy iProxy = WebRequest.GetSystemWebProxy();
WebProxy myProxy = new WebProxy();// potentially, setup credentials on the proxy heremyProxy.Credentials = CredentialCache.DefaultCredentials;
myProxy.UseDefaultCredentials = false;if (ProxyAddress.Text.Trim()!= "" && ProxyPort.Text.Trim()!= "")
{
 myProxy.Address = new Uri("http://" + ProxyAddress.Text.Trim() + 
 ":" + ProxyPort.Text.Trim());
}if (userName!= null && userName.Length >0)
{
 service.setUserCredentials(userName, passWord);
}// only get event's for today - 1 month until today + 1 yearquery.Uri = new Uri(calendarURI);
requestFactory.CreateRequest(GDataRequestType.Query, query.Uri);// = myProxy;if (calendarControl.SelectionRange!= null)
{
 query.StartTime = calendarControl.SelectionRange.Start.AddDays(-1) ;
 query.EndTime = calendarControl.SelectionRange.End.AddDays(1) ;
}else{
 query.StartTime = DateTime.Now.AddDays(-12);
 query.EndTime = DateTime.Now.AddMonths(0);
}
EventFeed calFeed = service.Query(query) as EventFeed;// now populate the calendarif (calFeed!= null && calFeed.Entries.Count == 0)
{
 MessageBox.Show("No Event found");
}else{
 while (calFeed!= null && calFeed.Entries.Count >0)
 {
 // look for the one with dinner time...foreach (EventEntry entry in calFeed.Entries)
 {
 this.entryList.Add(entry);
 if (entry.Times.Count >0)
 {
 foreach (When w in entry.Times)
 {
 dates.Add(w.StartTime);
 }
 }
 }
 // just query the same query again.if (calFeed.NextChunk!= null)
 {
 query.Uri = new Uri(calFeed.NextChunk);
 calFeed = service.Query(query) as EventFeed;
 }
 else calFeed = null;
 }
 DateTime[] aDates = new DateTime[dates.Count];
 int i = 0;
 foreach (DateTime d in dates)
 {
 aDates[i++] = d;
 }
 this.calendarControl.BoldedDates = aDates;
 // this.calendarControl.SelectionRange = // new SelectionRange(DateTime.Now, DateTime.Now);if (aDates.Length> 0)
 {
 MessageBox.Show("Please select the Dates marked" + 
 "bold in the calendar to see events");
 }
 else {
 MessageBox.Show("No Event found against selected dates rage and calendar");
 }
}try{
 EventEntry entry = new EventEntry();
 // Set the title and content of the entry. entry.Title.Text = EventName.Text;
 entry.Content.Content = Description.Text;
 // Set a location for the event. Where eventLocation = new Where();
 eventLocation.ValueString = location.Text;
 entry.Locations.Add(eventLocation);
 DateTime dtstartdatetime = calStartDate.Value;
 DateTime dtenddatetime = CalEndDate.Value;
 string[] str = new string[1];
 str[0] = ":";
 double dblHour = Convert.ToDouble(cmbStartTime.SelectedItem.ToString().Split(
 str, StringSplitOptions.RemoveEmptyEntries)[0]);
 double dblMinutes = Convert.ToDouble(cmbStartTime.SelectedItem.ToString().Split(
 str, StringSplitOptions.RemoveEmptyEntries)[1]);
 dtstartdatetime.AddHours(dblHour);
 dtstartdatetime.AddMinutes(dblMinutes);
 dblHour = Convert.ToDouble(cmbEndTime.SelectedItem.ToString().Split(
 str, StringSplitOptions.RemoveEmptyEntries)[0]);
 dblMinutes = Convert.ToDouble(cmbEndTime.SelectedItem.ToString().Split(
 str, StringSplitOptions.RemoveEmptyEntries)[1]);
 dtenddatetime.AddHours(dblHour);
 dtenddatetime.AddMinutes(dblMinutes);
 When eventTime = new When(dtstartdatetime, dtenddatetime);
 entry.Times.Add(eventTime);
 userName = UserName.Text;
 passWord = Password.Text;
 if (userName!= null && userName.Length >0)
 {
 service.setUserCredentials(userName, passWord);
 }
 Uri postUri;
 postUri = new Uri("http://www.google.com/calendar" + 
 "/feeds/default/private/full");
 if (GoogleCalendar.SelectedIndex >= 0)
 {
 postUri = new Uri("http://www.google.com/calendar/feeds/" + 
 ((CalendarEntry)(GoogleCalendar.SelectedItem)).SelfUri.ToString(
 ).Substring(((CalendarEntry)(
 GoogleCalendar.SelectedItem)).SelfUri.ToString(
 ).LastIndexOf("/") + 1) + "/private/full" );
 }
 GDataGAuthRequestFactory requestFactory = 
 (GDataGAuthRequestFactory)service.RequestFactory;
 IWebProxy iProxy = WebRequest.GetSystemWebProxy();
 WebProxy myProxy = new WebProxy();
 // potentially, setup credentials on the proxy here myProxy.Credentials = CredentialCache.DefaultCredentials;
 myProxy.UseDefaultCredentials = false;
 if (ProxyAddress.Text.Trim()!= "" && ProxyPort.Text.Trim()!= "")
 {
 myProxy.Address = new Uri("http://" + ProxyAddress.Text.Trim() + 
 ":" + ProxyPort.Text.Trim());
 }
 requestFactory.CreateRequest(GDataRequestType.Insert, postUri);// = myProxy;// Send the request and receive the response: AtomEntry insertedEntry = service.Insert(postUri, entry);
 MessageBox.Show("Event Successfully Added");
}catch (Exception ex)
{
 MessageBox.Show(ex.Message.ToString());
}

CAL  EVE  event  日历  事件  Google日历  
相关文章