2012-04-10 53 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 
相关问题