好的,所以我终于想出了如何构建和传递我的 JSON 字符串。我正在使用 VBJSON 来构建 JSON 字符串。请记住 JSON 区分大小写(或至少 Google 将其解释为区分大小写)。具有关键日期时间的一对与具有关键日期时间的一对不同,Google 将拒绝后者。
'Code to create JSON using Dictionary Objects and Collection Objects
Dim d As New Scripting.Dictionary
Dim c As New Collection
d.Add "kind", "calendar#event"
d.Add "summary", "Event Title/Summary"
Dim d2(4) As New Scripting.Dictionary
d2(0).Add "dateTime", "2012-04-14T16:00:00.000-04:00"
d.Add "start", d2(0)
d2(1).Add "dateTime", "2012-04-14T18:00:00.000-04:00"
d.Add "end", d2(1)
'First Attendee
d2(2).Add "email", "john.doe@gmail.com"
d2(2).Add "displayName", "John Doe"
d2(2).Add "organizer", True
d2(2).Add "self", True
'Add attendee to collection
c.Add d2(2)
'Second attendee
d2(3).Add "email", "suzy.doe@gmail.com"
d2(3).Add "displayName", "Suzy Doe"
'Add attendee to collection
c.Add d2(3)
'Add collection to original/primary dictionary object
d.Add "attendees", c
'Add more nested pairs to original/primary dictionary object
d2(4).Add "useDefault", True
d.Add "reminders", d2(4)
'Now output the JSON/results
'This requires the VBJSON module (named just JSON, a module, not a class module)
Debug.Print JSON.JSONToString(d)
未经修饰的输出是这样的:
{"kind":"calendar#event","summary":"Event Title\/Summary","start":{"dateTime":"2012-04-14T16:00:00.000-04:00"},"end":{"dateTime":"2012-04-14T18:00:00.000-04:00"},"attendees":[{"email":"john.doe@gmail.com","displayName":"John Doe","organizer":true,"self":true},{"email":"suzy.doe@gmail.com","displayName":"Suzy Doe"}],"reminders":{"useDefault":true}}
然后是使用 Google Calendar API V3 将其提交给 Google 的方法。在 V3 中,您必须使用 OAuth2.0,因此您需要有一个有效的访问令牌才能附加到您的 URL,如下所示。您还需要知道您的 CalendarID,它通常是您的电子邮件地址 URL 编码。例如,您的 calendarid 如下所示:john.doe%40gmail.com
Dim objXMLHTTP As MSXML2.ServerXMLHTTP
Set objXMLHTTP = New MSXML2.ServerXMLHTTP
Dim sPostData As String
sPostData = JSON.JSONToString(d)
Dim sURL As String
sURL = "https://www.googleapis.com/calendar/v3/calendars/{mycalendarid}/events?sendNotifications=false&fields=etag%2ChtmlLink%2Cid&pp=1&access_token={my oauth2.0 access token}"
With objXMLHTTP
.Open "POST", sURL, False
.setRequestHeader "Content-Type", "application/json"
.Send (sPostData)
End With
Debug.Print objXMLHTTP.ResponseText
Set objXMLHTTP = Nothing