好的,所以我终于想出了如何构建并传递我的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