好的,所以我終於想出瞭如何構建並傳遞我的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", "[email protected]"
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", "[email protected]"
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)
的unprettified輸出是這樣的:
{"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":"[email protected]","displayName":"John Doe","organizer":true,"self":true},{"email":"[email protected]","displayName":"Suzy Doe"}],"reminders":{"useDefault":true}}
然後在這裏是一個使用谷歌日曆API的V3你是如何提交給谷歌。在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
您的日期格式是關閉的,我認爲:在谷歌的例子是這樣的 - '「創造」:「2011-05-23T22: 27:01.000Z「'在VBA中,你可以使用xmlhttp和POST發送該json。 – 2012-04-10 16:19:53
那麼我該在日期前加上引號嗎?我對JSON中的引號和數據類型感到困惑。 JSON最終只是一個字符串數據類型? – HK1 2012-04-10 16:54:25
查看Google文檔中的示例:這就是json應該看起來的樣子。我不會說JSON是一種「數據類型」 - 更像是一種數據交換格式。你應該花一些時間在這裏:http://www.json.org/ – 2012-04-10 17:05:05