2012-04-10 70 views
3

我試圖使用Google API V3向Google日曆添加事件。我沒有使用任何谷歌的圖書館,所以我很困惑如何做到這一點。 Google's documentation告訴我,我需要在請求正文中發送"Events Resource"。我認爲我已經構建了一個有效的JSON「字符串」,但我不知道如何正確準備並將其發送到所謂的「請求主體」中。令我困惑的是字符串值被包裹在引號中的方式,而非字符串值則不是。這是否意味着我需要將整個事件作爲一個字符串包裝,並且每個雙引號需要雙/四個引號?爲Google日曆API V3構建JSON使用VBA

下面是我寫的JSON,但我還沒弄明白怎麼還這個傳遞給谷歌,所以我一直無法對其進行測試:

{ 
    "kind": "calendar#event", 
    "start": { 
    "dateTime": 04/10/2012 08:00 AM 
    }, 
    "end": { 
    "dateTime": 04/10/2012 08:00 AM 
    }, 
    "attendees": [ 
    { 
     "email": "[email protected]", 
     "displayName": "My Name", 
     "organizer": True, 
     "self": True 
    } 
    ], 
    "reminders": { 
    "useDefault": True 
    } 
} 

我有一個VBJSON代碼模塊安裝在我的Access/VBA數據庫中。我在那裏看到一個名爲StringToJSON的函數,它返回一個字符串。我仍然感到困惑。當我將這個JSON傳遞給Google時,它是否僅僅是我的代碼中的一個大字符串值?

+0

您的日期格式是關閉的,我認爲:在谷歌的例子是這樣的 - '「創造」:「2011-05-23T22: 27:01.000Z「'在VBA中,你可以使用xmlhttp和POST發送該json。 – 2012-04-10 16:19:53

+0

那麼我該在日期前加上引號嗎?我對JSON中的引號和數據類型感到困惑。 JSON最終只是一個字符串數據類型? – HK1 2012-04-10 16:54:25

+0

查看Google文檔中的示例:這就是json應該看起來的樣子。我不會說JSON是一種「數據類型」 - 更像是一種數據交換格式。你應該花一些時間在這裏:http://www.json.org/ – 2012-04-10 17:05:05

回答

1

好的,所以我終於想出瞭如何構建並傳遞我的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