65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 Google 数据协议在 Google 日历中创建事件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (4投票s)

2010年10月26日

CPOL

1分钟阅读

viewsIcon

42624

downloadIcon

853

用于添加、删除或修改 Google 日历事件的模板

引言

本文将向用户展示如何使用 Google 数据协议添加、编辑和删除 Google 日历中的事件。

背景

Google .NET 客户端库开发者指南中的示例是用 C# 编写的。作者已将 Google 数据 API(“GData”)查询翻译成 VB.NET。

Google 数据 API 开发者指南:.NET 是学习 Google 日历 API 的绝佳资源。

Using the Code

要使用下面的源代码,您需要执行以下操作:

下载 Google .NET 客户端库

创建一个 Google 电子邮件帐户和一个 Google 日历。

设置对以下 DLL 的引用(位于 C:\Program Files (x86)\Google\Google Data API SDK\Redist 目录中)。

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

创建一个名为 CalendarProperties.vb 的新类模块,并添加以下代码

Public Enum Request
AddEvent
ChangeEvent
DeleteEvent
End Enum

Public AuthorName As String
Public AuthorEmail As String
Public Title As String
Public TitleUpdate As String
Public Content As String
Public EmailAddress As String
Public EmailPassword As String
Public ServiceName As String
Public CalendarID
Public StartDate As Date
Public EndDate As Date
Public StartTime As String
Public EndTime As String
Public EventLocation As String
Public CalendarPrivateURI As String
Public RequestType As Integer

创建第二个名为 CalendarEvents.vb 的类模块,并将导入语句添加到页面顶部。将以下代码添加到模块中

Imports Google.GData.Client 
Imports Google.GData.Calendar 
Imports Google.AccessControl
Public Shared Function CalendarEvent(ByVal CP As CalendarProperties) As String

 'Set the service 
 'Important: The service name must be in this form exactly or you will get a 401 error
 Dim Service As New Service("Cl", "MyCompany-MyApplication-MyVersion")
 Service.setUserCredentials(CP.EmailAddress, CP.EmailPassword)

 'Set the specific calendar to update using the calendar id
 'the calendar id is on the calendar setting page
 Dim CalendarUri As String = "http://www.google.com/calendar/feeds/" & _
				CP.CalendarID & "/private/full"

 'Add Event
 If CP.RequestType = CP.Request.AddEvent Then

 Try

'Set the location
Dim location As New Where
location.ValueString = CP.EventLocation

'Add location to the entry
Dim entry As New EventEntry
entry.Locations.Add(location)

'Set the dates and times
Dim mydateString As String = CP.StartDate & " " & CP.StartTime
Dim StartDate As Date = Date.Parse(mydateString, _
System.Globalization.CultureInfo.InvariantCulture)

mydateString = CP.EndDate & " " & CP.EndTime
Dim EndDate As Date = Date.Parse(mydateString, _
System.Globalization.CultureInfo.InvariantCulture)

'Add dates to the entry
Dim eventTime As New [When](StartDate, EndDate)
entry.Times.Add(eventTime)

'Set the author
Dim author As New AtomPerson(AtomPersonType.Author)
author.Name = CP.AuthorName
author.Email = CP.AuthorEmail

'Add the author to the entry
entry.Authors.Add(author)

'Add title
entry.Title.Text = CP.Title

'Add content
entry.Content.Content = CP.Content

'Set the Uri 
Dim posturi As New Uri(CalendarUri)

'Send the request
Dim insertedEntry As New AtomEntry
Service.Insert(posturi, entry)


Catch ex As Exception
Return ex.ToString
End Try
Return "OK"
End If

If CP.RequestType = CP.Request.DeleteEvent Then

'Set the query
Dim myquery As New EventQuery(CalendarUri)

Try
'Query the title
myquery.Query = CP.Title

Dim myresults As AtomFeed = Service.Query(myquery)
Dim i As Integer

'Delete the matched titles
For i = 0 To myresults.Entries.Count - 1
 Dim FirstMatch As AtomEntry = myresults.Entries(i)
 FirstMatch.Delete()

Next

Catch ex As Exception
Return ex.ToString
End Try
Return "OK"

End If

If CP.RequestType = CP.Request.ChangeEvent Then
Dim myquery As New EventQuery(CalendarUri)
Try
 myquery.Query = CP.Title

Dim myresults As AtomFeed = Service.Query(myquery)
Dim i As Integer

'Update the matched titles
For i = 0 To myresults.Entries.Count - 1
Dim FirstMatch As AtomEntry = myresults.Entries(i)
FirstMatch.Title.Text = CP.TitleUpdate
FirstMatch.Update()

Next

Catch ex As Exception
Return ex.ToString
End Try
Return "OK"

End If

Return "Error set the request type."

End Function

接下来,创建一个名为 CalendarWebForm.aspx 的 Web 表单。将以下代码添加到创建的 CalendarWebForm.aspx.vp 文件中。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
	Handles Me.Load
 Dim CP As New CalendarProperties
 Dim CE As New CalendarEvents

 'Set the author 
 CP.AuthorEmail = "Authors email address"
 CP.AuthorName = "Authors name"

 'Set the private address for the calendar. The private address can be found
 'on the calendars settings page next to private address. Click the HTML button
 'and copy the address. This can be used to give read only access to the calendar
 CP.CalendarPrivateURI = "http://PrivateAddres"

 CP.Content = "This is the event content"

 'Your email address used to create the calendar
 CP.EmailAddress = "YourName@gmail.com"

 'Your email password
 CP.EmailPassword = "YourPassword"

 CP.EndDate = "10/25/2010"
 CP.EndTime = "11:00:00 AM"
 CP.StartDate = "10/25/2010"
 CP.StartTime = "8:00:00 AM"
 CP.Title = "Important Meeting"

 'Set the calendar ID the calendar ID can be found on the calendars settings page 
 'next to the calendar address. The calendar ID is used to construct the full URL
 CP.CalendarID = "Your Calendar ID"

 CP.EventLocation = "Your event address"

 'Use this for new events
 CP.RequestType = CP.Request.AddEvent

 'Use this for changes to the title
 'CP.TitleUpdate = "Important Meeting Over"
 'CP.RequestType = CP.Request.ChangeEvent

 'Use this to delete the event. The title must match the event title exactly.
 'CP.RequestType = CP.Request.DeleteEvent

 Dim ReturnString As String = CE.CalendarEvent(CP)
 System.Diagnostics.Debug.Print(ReturnString)

 Response.Redirect(CP.CalendarPrivateURI)

End Sub

如果您下载了源代码文件,请务必将类模块放在 app_code 目录中。

当运行项目时,您将被重定向到您的 Google 日历。

希望这能帮助您开始使用 Google 日历。

历史

  • 版本 1.0
© . All rights reserved.