构建JSON对于谷歌日历API V3使用VBA [英] Build JSON For Google Calendar API V3 Using VBA

查看:335
本文介绍了构建JSON对于谷歌日历API V3使用VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个事件添加到使用谷歌API V3谷歌日历。我不使用任何谷歌的图书馆,所以我真的感到困惑,如何做到这一点。 谷歌的文档告诉我,我需要发送一个< A HREF =htt​​ps://developers.google.com/google-apps/calendar/v3/reference/events#resource相对=nofollow>活动资源在请求主体。我想我已经建立了一个有效的JSON串,但我不知道如何正确地prepare,并在所谓的请求体发送。什么混淆我的是字符串值被包裹在报价和非字符串值没有办法的办法。这是否意味着我需要包住整个事情作为一个字符串和双/四引号每个双引号?

I'm trying to add an event to a Google Calendar using the Google API V3. I'm not using any of Google's libraries so I'm really baffled as to how to do this. Google's documentation tells me that I need to send an "Events Resource" in the request body. I think I've built a valid JSON "string" but I have no idea how to properly prepare it and send it in the so-called "request body". What confuses me is the way that string values are wrapped in quotes and non-string values are not. Does this mean that I'll need to wrap the entire thing as a string and double/quadruple quote each double quote?

下面是我写的JSON,但我还没有想出尚未如何通过这谷歌,所以我一直无法对其进行测试:

Here's the JSON I've written but I haven't figure out yet how to pass this to Google so I haven't been able to test it:

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

我有一个VBJSON code模块安装在我的访问/ VBA数据库。我看到有一个函数调用StringToJSON并返回一个字符串。我仍然百思不得其解。当我通过这个JSON谷歌,将它仅仅是在我的code一个大的字符串值?

I do have a VBJSON code module installed in my Access/VBA Database. I see a function in there called StringToJSON which returns a string. I'm still baffled. When I pass this JSON to Google, will it merely be one big string value within my code?

推荐答案

好了,我终于想通了如何建立和通过我的JSON字符串。我使用VBJSON打造的JSON字符串。请记住,JSON是大小写敏感的(或至少是谷歌内部$ P $点是区分大小写)。一对与关键日期时间是不一样的一对与主要datetime和谷歌将拒绝后者。

OK, so I finally figured out how to build and pass my JSON string. I'm using VBJSON to build the JSON string. Please remember that JSON is case sensitive (or at least Google interprets it case sensitive). A pair with the key dateTime is not the same as a pair with the key datetime and Google will reject the latter.

'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)

联合国prettified输出是这样的:

The unprettified output is this:

{"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}}

和那么这里是你如何使用谷歌日历的API V3提交给谷歌。在V3你有那么你需要有一个有效的访问令牌附加到您的网址如下所示使用OAuth2.0。你还需要知道你的CalendarID通常是您的电子邮件地址,URL连接codeD。例如,你的calendarid看起来就像这样:john.doe%40gmail.com

And then here's how you submit it to Google using V3 of the Google Calendar API. In V3 you have to use OAuth2.0 so you need to have a valid Access Token to append to your URL as shown below. You'll also need to know your CalendarID which is usually your email address URL encoded. For example, your calendarid will look like this: 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

这篇关于构建JSON对于谷歌日历API V3使用VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆