Last active
March 8, 2024 20:19
-
-
Save timhall/2734e7d858a7d8c54360 to your computer and use it in GitHub Desktop.
VBA-Web - Google Calendar API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' (Based of Analytics example) | |
' In Client setup, set BaseUrl and Scope for Calendar | |
Client.BaseUrl = "https://www.googleapis.com/calendar/v3/" | |
Auth.AddScope "calendar" | |
Public Type CalendarEvent | |
Summary As String | |
Location As String | |
Attendees As Collection | |
StartTime As Date | |
EndTime As Date | |
End Type | |
' Insert event | |
' https://developers.google.com/google-apps/calendar/v3/reference/events/insert | |
Public Function InsertEvent(CalendarId As String, Event As CalendarEvent) As WebResponse | |
Dim Request As New WebRequest | |
Request.Resource = "calendars/{calendarId}/events" | |
Request.Method = WebMethod.HttpPost | |
Request.AddUrlSegment "calendarId", CalendarId | |
Dim Body As New Dictionary | |
Body.Add "summary", Event.Summary | |
Body.Add "location", SummaryLocation | |
Body.Add "attendees", Event.Attendees | |
Body.Add "start", New Dictionary | |
Body("start").Add "dateTime", Event.StartTime | |
Body.Add "end", New Dictionary | |
Body("end").Add "dateTime", Event.EndTime | |
Set Request.Body = Body | |
Set InsertEvent = Client.Execute(Request) | |
' https://developers.google.com/google-apps/calendar/recurringevents is a good example of what it should look like | |
' -> POST .../calendars/calendar-id/events | |
' { | |
' "summary": "...", | |
' "location": "...", | |
' "start": { | |
' "dateTime": "...T...Z (Date automatically converted to UTC" | |
' (shouldn't need timeZone with UTC dates, but Google may require it) | |
' }, | |
' ... | |
' } | |
End Function | |
Public Sub Test | |
Dim Event As CalendarEvent | |
Event.Summary = "Staff Meeting" | |
Event.Location = "Conference Room" | |
Event.StartTime = DateValue("Jan. 1, 2015") + TimeValue("8:00 AM") | |
Event.EndTime = DateAdd("h", 1, Event.StartTime) | |
Set Event.Attendees = New Collection | |
Event.Attendees.Add New Dictionary | |
Event.Attendees(1).Add "email", "bob@company.com" | |
Event.Attendees.Add New Dictionary | |
Event.Attendees(2).Add "email", "sally@company.com" | |
Dim Response As WebResponse | |
Set Response = InsertEvent("StaffCalendar", Event)) | |
If Response.StatusCode = WebStatusCode.Ok Then | |
' Success! | |
End If | |
End Sub |
Hi! Same issue here. Should maybe an extra header be added for the authentication token within Prepare method of WebRequest class?
Public Sub Prepare()
' Add/replace general headers for request
SetHeader "User-Agent", Me.UserAgent
SetHeader "Accept", Me.Accept
If Me.Method <> WebMethod.HttpGet Then
SetHeader "Content-Type", Me.ContentType
SetHeader "Content-Length", VBA.CStr(Me.ContentLength)
End If
End Sub
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Tim. Excellent code. I did implement the module like Analytics module, tested the sub Test, but happened one error in "Set InsertEvent = Client.Execute(Request)" . Can you help me?