使用 Google 日历与 VB.NET 和 ASP.NET
如何从 VB.NET 使用 Google 日历 API。
引言
我搜索了又搜索,但找不到任何完整的示例,展示如何直接使用 Google 日历 API。因此,在从许多不同的示例中拼凑出我的解决方案后,我决定发布这个小型项目,既是为了巩固我自己的方法,也是为了帮助其他可能也需要这样做的人。
背景
我有几篇类似的参考文章,其中一篇是在 The Code Project 上发布的:使用 Google 日历与 ASP.NET,作者是 Igor Alekseev。
事实上,在我找到他的文章之前,我一直很难让我的事件进入我希望它们进入的特定日历,而 Igor 的代码是我遇到的第一个清楚地定义了 Google 服务哪些 URIs 的代码。
我有点模仿了 Igor 的方法,将 CalendarService
对象 ByRef
传递给后续函数,因为这对于避免被 Google 禁封至关重要,这可能会在您的测试过程中产生一些有趣的错误!
然而,最重要的是,也许是他对这行代码的直觉
calFeed.Entries(x).Id.AbsoluteUri.Substring(63)
在我发现 "substring(63)
" 之前,我一直很难获得可靠的日历 ID!
谢谢 Igor!
Using the Code
我努力在这个代码中维护两种不同的方法,一种是单次调用,另一种是复合调用... 单次调用用于我自己的应用程序,例如,当向我们的数据库表中添加新行时。我可以进行一次调用并将该数据推送到日历... 这种方法也使其非常适合 Web 服务调用。当您需要从客户端代码执行多个步骤时,另一种方法很好,它允许您在一个调用中获取您的 CalendarService
对象,然后在您的后续操作中持续重用它,这样您的登录就不会针对 Google 每次调用都重复。
单次调用通常是这样的
Dim g As New GoogleCalSvc, str as String
str = g.A1ListMyCalendars
而多次或复合调用可能看起来像这样
Dim g As New GoogleCalendar_KellyTours, str as string
str = str & g.FindEvent(g.GetCalendarService, "123")
或者甚至像这样,其中服务对象 s
被重复使用
Dim g As New GoogleCalSvc
Dim s = g.GetCalendarService
Dim out As String = g.FindEvent(s, "Primary", "123")
out = out & g.FindEvent(s, "Primary", "456")
out = out & g.FindEvent(s, "Primary", "789")
Me.output.InnerHtml = out
关注点
一旦你克服了互联网上大多数代码和文档都是针对 C# 项目的事实,稍加努力,你最终会发现该 API 实际上非常容易使用。随着持续的开发,这个项目可以很容易地发展成为一个完整的日历访问封装接口。
在我自己的使用中,我也有额外的数据库代码就在这个文件中,所以我可以只将记录 ID 传递给一些函数,它们会自动以一种相当实时的模式将所需记录发布、更新等,到/从日历。
代码列表
Imports System.IO
Imports System.Data.SqlClient
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
'Imports Google.GData.AccessControl
Imports Google.GData.Calendar
Imports Google.GData.Client
Imports Google.GData.Extensions
' To allow this Web Service to be called from script,
' using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="https:///GoogleCalendarInVBNET/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class GoogleCalSvc
Inherits System.Web.Services.WebService
Private Const userName As String = "SomeUser@GMailOrGoogleAppsDomain.com"
Private Const userPassword As String = "SomeStrongPassword"
'' Here's the feed to access events on the users' private/primar calendar:
Private feedUri As String = _
"https://www.google.com/calendar/feeds/default/private/full?max-results=9999"
'' Here's the feed that lists all calendars that this user has access to:
Private Const feedOwnCalendars As String = _
"https://www.google.com/calendar/feeds/default/owncalendars/full"
'' This is a feed for non-primary calendars that requires an ID field,
'' taken from enumerating the users' calendars...
Private Const CALENDAR_TEMPLATE As String = _
"https://www.google.com/calendar/feeds/{0}/private/full?max-results=9999"
#Region "Single Call Public Functions"
<WebMethod()> _
Public Function A1ListMyCalendars() As String
Dim str As String
str = ListAllCalendars()
Return str
End Function
<WebMethod()> _
Public Function A1CreateEvent(ByVal stCalName As String, ByVal strWhere As String, _
ByVal strStart As String, ByVal strEnd As String, _
ByVal strSubject As String, ByVal strBody As String, _
ByVal strRecurData As String) As String
Return CreateEvent(GetCalendarService, stCalName, strWhere, _
strStart, strEnd, strSubject, strBody, strRecurData)
End Function
<WebMethod()> _
Public Function A1DeleteEvent(ByVal intTripNo As Integer, ByVal stCalName As String) As String
Return DeleteEvent(GetCalendarService, intTripNo, stCalName)
End Function
<WebMethod()> _
Public Function A1ListUsersCalendars() As String
Dim Service As CalendarService = GetCalendarService()
Dim query As New FeedQuery
query.Uri = New Uri(feedUri) ' Private feedUri 'cause for users calendars
Dim calFeed As AtomFeed
calFeed = Service.Query(query)
Dim str As String = ""
For x As Integer = 0 To calFeed.Entries.Count - 1
str = str & calFeed.Entries(x).Title.Text
If calFeed.Entries.Count > 1 And x < (calFeed.Entries.Count - 1) Then
str = str & ", " & calFeed.Title.Text & vbCrLf
End If
Next
Return str
End Function
#End Region
#Region "Public GET Functions"
Public Function GetCalendarService() As CalendarService
Dim Service As CalendarService = New CalendarService("My-exampleApp-1")
Service.setUserCredentials(userName, userPassword)
Return Service
End Function
<WebMethod()> _
Public Function GetCalendarID(ByRef Service As CalendarService, ByVal strCalName As String) As String
Dim query As New FeedQuery
Dim str As String = ""
query.Uri = New Uri(feedOwnCalendars)
Dim calFeed As AtomFeed
calFeed = Service.Query(query)
For x As Integer = 0 To calFeed.Entries.Count - 1
If calFeed.Entries(x).Title.Text = strCalName Then
str = calFeed.Entries(x).Id.AbsoluteUri.Substring(63)
End If
Next
Return str
End Function
<WebMethod()> _
Public Function GetCalendarURI(ByRef Service As CalendarService, ByVal stCalName As String) As String
Dim stURI As String = String.Format(CALENDAR_TEMPLATE, GetCalendarID(Service, stCalName))
'' Easy override to go back to the users' primary calendar!!!
If stCalName = "Primary" Then
stURI = feedUri
End If
Return stURI
End Function
#End Region
<WebMethod()> _
Public Function ListAllCalendars() As String
Dim Service As CalendarService = GetCalendarService()
Dim query As New FeedQuery
query.Uri = New Uri(feedOwnCalendars)
Dim calFeed As AtomFeed
calFeed = Service.Query(query)
Dim str As String = ""
For x As Integer = 0 To calFeed.Entries.Count - 1
str = str & calFeed.Entries(x).Title.Text & "|" & _
calFeed.Entries(x).Id.AbsoluteUri.Substring(63) & vbCrLf
Next
Return str
End Function
<WebMethod()> _
Public Function DeleteAllEvents(ByVal strCalName As String) As String
Dim str As String = ""
Dim Service As CalendarService = GetCalendarService()
Dim query As New FeedQuery
query.Uri = New Uri(GetCalendarURI(Service, strCalName))
Dim calFeed As AtomFeed
calFeed = Service.Query(query)
If calFeed.Entries.Count > 0 Then
For x As Integer = 0 To calFeed.Entries.Count - 1
str = str & calFeed.Entries(x).Title.Text & vbCrLf
calFeed.Entries(x).Delete()
Next
End If
Return str
End Function
Public Function DeleteEvent(ByRef Service As CalendarService, _
ByVal strSearchFor As String, ByVal stCalName As String) As String
Dim str As String = ""
Dim query As New FeedQuery
query.Uri = New Uri(GetCalendarURI(Service, stCalName))
query.Query = strSearchFor
Dim sFeed As EventFeed = Service.Query(query)
Dim Exists As Boolean = False
If sFeed.TotalResults > 0 Then
Exists = True
Dim calFeed As AtomFeed
calFeed = Service.Query(query)
If calFeed.Entries.Count > 0 Then
For x As Integer = 0 To calFeed.Entries.Count - 1
str = str & calFeed.Entries(x).Title.Text
calFeed.Entries(x).Delete()
Next
End If
End If
Return str
End Function
Private Function CreateEvent(ByRef Service As CalendarService, _
ByVal strCalName As String, _
ByVal strWhere As String, _
ByVal strStart As String, _
ByVal strEnd As String, _
ByVal strSubject As String, _
ByVal strBody As String, _
ByVal strRecurData As String) As String
Dim entry As New EventEntry
entry.Title.Text = strSubject
entry.Content.Content = strBody
Dim eventLocation As New Where
eventLocation.ValueString = strWhere
entry.Locations.Add(eventLocation)
Dim eventTime As New [When](strStart, strEnd)
entry.Times.Add(eventTime)
Dim postUri As Uri = New Uri(GetCalendarURI(Service, strCalName))
Dim insertedEntry As AtomEntry = Service.Insert(postUri, entry)
Return insertedEntry.ToString
End Function
Public Function FindEvent(ByRef Service As CalendarService, _
ByVal strCalName As String, ByVal strSearchFor As String) As String
Dim strOut As String = ""
Dim query As EventQuery = New EventQuery
query.Uri = New Uri(GetCalendarURI(Service, strCalName))
query.Query = strSearchFor.ToString
Dim calFeed As EventFeed = Service.Query(query)
Dim feedEntry As EventEntry
If calFeed.TotalResults > 0 Then
For Each feedEntry In calFeed.Entries
strOut = strOut & (feedEntry.Title.Text) & vbCrLf
Next
Else
strOut = "No Results found"
End If
Return strOut
End Function
<WebMethod()> _
Public Function ListUserEvents() As String
Dim strOut As String = ""
Dim query As EventQuery = New EventQuery
Dim service As CalendarService = GetCalendarService()
query.Uri = New Uri(GetCalendarURI(service, "Primary"))
Dim calFeed As EventFeed = service.Query(query)
Dim feedEntry As EventEntry
For Each feedEntry In calFeed.Entries
strOut = strOut & (feedEntry.Title.Text) & vbCrLf
Next
Return strOut
End Function
<WebMethod()> _
Public Function ListEvents(ByVal strCalName As String) As String
Dim strOut As String = ""
Dim query As EventQuery = New EventQuery
Dim Service As CalendarService = GetCalendarService()
query.Uri = New Uri(GetCalendarURI(Service, strCalName))
Dim calFeed As EventFeed = Service.Query(query)
Dim feedEntry As EventEntry
For Each feedEntry In calFeed.Entries
strOut = strOut & (feedEntry.Title.Text) & vbCrLf
' list the event title in a messagebox...
Next
Return strOut
End Function
<WebMethod()> _
Public Function DeDupeEvents(ByVal strCalName As String) As String
Dim strOut As String = "", strVal As String = ""
Dim vals As New Collection
' list all the events....
Dim query As EventQuery = New EventQuery
Dim service As CalendarService = GetCalendarService()
query.Uri = New Uri(GetCalendarURI(service, strCalName))
Dim calFeed As EventFeed = service.Query(query)
Dim feedEntry As EventEntry
For Each feedEntry In calFeed.Entries
strVal = (feedEntry.Title.Text)
If Not vals.Contains(strVal) Then
strOut = strOut & strVal
vals.Add(strVal, strVal)
Else
feedEntry.Delete()
End If
Next
Return strOut
End Function
End Class
历史
- 2011年12月31日 - 这是我第一次发布这篇文章和代码,并且我相信在将其与我应用程序的其余部分分开之后,将需要进行一些更新。