具有离线访问权限的Google Calendar Oauth

时间:2015-01-06 06:43:24

标签: vbscript oauth google-calendar-api

我正在尝试使用VBScript和Oauth脱机访问来访问Google Calendar API。我设法获得了刷新令牌和访问令牌,现在我想使用以下代码进入实际使用API​​的步骤:

sUrl="https://www.googleapis.com/calendar/v3/users/me/calendarList"
sRequest = "access_token="&accesstoken

response=HTTPPost (sUrl, sRequest)

Function HTTPPost(sUrl, sRequest)
    set oHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    oHTTP.open "POST", sUrl,false
    oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    oHTTP.setRequestHeader "Content-Length", Len(sRequest)
    oHTTP.send sRequest
    HTTPPost = oHTTP.responseText
End Function

我得到的回应是:

    {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

即使访问令牌有效 - 如果我将sURL更改为https://www.googleapis.com/oauth2/v1/tokeninfo并运行脚本,我会得到以下响应,显示有效令牌:

{
"issued_to": "...",
 "audience": "...,
"scope": "https://www.googleapis.com/auth/calendar",
"expires_in": 2568,
"access_type": "offline"
}

如果我将网址https://www.googleapis.com/calendar/v3/users/me/calendarList?access_token= ...粘贴到浏览器中,我会收到Google的有效回复,其中列出了用户的日历

3 个答案:

答案 0 :(得分:0)

您的代码使用HTTP POST,但需要使用HTTP GET请求,如下所示:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list。此外,您可以在access_token参数中提供访问令牌(而不是在另一个答案中建议的oauth_token参数中),但另一个选项是在Authorization标头中提供它,如: / p>

Authorization: Bearer <token>

作为示例,从:

返回的令牌
curl -v https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=<token>`

以下内省结果:

{
 "issued_to": "<...>.apps.googleusercontent.com",
 "audience": "<...>.apps.googleusercontent.com",
 "user_id": "<...>",
 "scope": "<> https://www.googleapis.com/auth/calendar",
 "expires_in": 3527,
 "email": "<...>",
 "verified_email": true,
 "access_type": "offline"
}

可用于Calendar API网址:

curl -v -H "Authorization: Bearer <>" https://www.googleapis.com/calendar/v3/users/me/calendarList

这样它就会给我一个JSON日历数据列表:

{
 "kind": "calendar#calendarList",
 "etag": "\"1420511621463000\"",
 "nextSyncToken": "00001420511621463000",
 "items": [
  {
   "kind": "calendar#calendarListEntry",
   "etag": "\"1418127331044000\"",
...

答案 1 :(得分:0)

  

401:凭据无效

     

无效的授权标头。您正在使用的访问令牌也是   已过期或无效。

访问令牌仅适用于1小时。小时结束后,访问令牌已过期,您需要使用刷新令牌来请求新的令牌。

获取新的访问令牌的原始代码看起来像这样。密钥是grant_type=refresh_token,这告诉身份验证服务器您正在请求新令牌。

https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token

答案 2 :(得分:0)

我设法解决了这个问题 - calendarList API需要发送一个空体,因此我将代码更改为:

set oHTTP = CreateObject("MSXML2.ServerXMLHTTP")
oHTTP.open "GET", sUrl&srequest,false
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.send

getcal = oHTTP.responseText