我正在尝试使用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 回答
访问令牌仅适用于1小时 . 小时结束后,访问令牌已过期,您需要使用刷新令牌来请求新的令牌 .
获取新的访问令牌的原始代码看起来像这样 . 密钥是
grant_type=refresh_token
这告诉身份验证服务器您正在请求新令牌 .您的代码使用HTTP POST,但需要使用HTTP GET请求,如下所示:https://developers.google.com/google-apps/calendar/v3/reference/calendarList/list . 此外,您可以在
access_token
参数中提供访问令牌(而不是在另一个答案中建议的oauth_token
参数中),但另一个选项是在Authorization标头中提供它,如:作为示例,从以下位置返回的令牌:
以下内省结果:
可以用于Calendar API URL:
所以它给了我一个JSON日历数据列表:
我设法解决了这个问题 - calendarList API需要发送一个空体,因此我将代码更改为:
getcal = oHTTP.responseText