从 Google 脚本控制台通过 Hangouts API 调用 POST 请求
Call POST request via Hangouts API from Google Scripts console
我已经通过环聊创建了一个机器人 API,现在想向 Google 日历 API 发送 POST
请求 (Freebusy: query)通过 Google 脚本控制台:
function testPOST() {
var url = "https://www.googleapis.com/calendar/v3/freeBusy";
var datetimeMin = "2018-02-22T18:00:00.000Z";
var datetimeMax = "2018-02-22T20:00:00.000Z";
var payload =
{
"timeMin": datetimeMin,
"timeMax": datetimeMax,
"timeZone": "Europe/Paris",
"groupExpansionMax": 2,
"calendarExpansionMax": 5,
"items": [
{
//I read, that Gmail address is your default calendar ID
"id": "%my_email@gmail.com%"
}
]
}
var options =
{
"method": "POST",
"payload": payload,
"followRedirects": true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.name);
Logger.log(params.blog);
} else {
Logger.log(result);
}
}
执行期间result
是null
或undefined
,Google控制台显示此对象,但它是空的。查了一下,指定邮箱的默认日历是public.
有人能弄清楚如何通过 Google 脚本控制台从 Hangouts API 发送 POST
请求吗?
日历 API 端点可能期望负载是 JSON 编码的字符串。因此,按如下方式更新您的选项对象:
var options = {
"method":"POST",
"contentType":"application/json",
"payload":JSON.stringify(payload)
}
实际上代码存在多个问题:
url
没有包含正确的 Google 日历 API 键
结果未解析为JSON
正确代码:
function testPOST() {
var url = 'https://www.googleapis.com/calendar/v3/freeBusy?key=%GOOGLE_CALENDAR_API_KEY%';
var datetimeMin = "2018-02-22T18:00:00.000Z";
var datetimeMax = "2018-02-22T20:00:00.000Z";
var payload = {
"timeMin": datetimeMin,
"timeMax": datetimeMax,
"timeZone": "Europe/Paris",
"groupExpansionMax": 2,
"calendarExpansionMax": 5,
"items": [
{
"id": "%my_email@gmail.com%"
}
]
}
var options = {
'method': "POST",
"payload": payload,
"muteHttpExceptions": true,
"payload": JSON.stringify(payload),
"contentType": 'application/json',
"timeZone": "CEST",
};
var result = UrlFetchApp.fetch(url, options);
Logger.log(result);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.name);
Logger.log(params.blog);
} else {
Logger.log(result);
}
}
我已经通过环聊创建了一个机器人 API,现在想向 Google 日历 API 发送 POST
请求 (Freebusy: query)通过 Google 脚本控制台:
function testPOST() {
var url = "https://www.googleapis.com/calendar/v3/freeBusy";
var datetimeMin = "2018-02-22T18:00:00.000Z";
var datetimeMax = "2018-02-22T20:00:00.000Z";
var payload =
{
"timeMin": datetimeMin,
"timeMax": datetimeMax,
"timeZone": "Europe/Paris",
"groupExpansionMax": 2,
"calendarExpansionMax": 5,
"items": [
{
//I read, that Gmail address is your default calendar ID
"id": "%my_email@gmail.com%"
}
]
}
var options =
{
"method": "POST",
"payload": payload,
"followRedirects": true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.name);
Logger.log(params.blog);
} else {
Logger.log(result);
}
}
执行期间result
是null
或undefined
,Google控制台显示此对象,但它是空的。查了一下,指定邮箱的默认日历是public.
有人能弄清楚如何通过 Google 脚本控制台从 Hangouts API 发送 POST
请求吗?
日历 API 端点可能期望负载是 JSON 编码的字符串。因此,按如下方式更新您的选项对象:
var options = {
"method":"POST",
"contentType":"application/json",
"payload":JSON.stringify(payload)
}
实际上代码存在多个问题:
url
没有包含正确的 Google 日历 API 键结果未解析为JSON
正确代码:
function testPOST() {
var url = 'https://www.googleapis.com/calendar/v3/freeBusy?key=%GOOGLE_CALENDAR_API_KEY%';
var datetimeMin = "2018-02-22T18:00:00.000Z";
var datetimeMax = "2018-02-22T20:00:00.000Z";
var payload = {
"timeMin": datetimeMin,
"timeMax": datetimeMax,
"timeZone": "Europe/Paris",
"groupExpansionMax": 2,
"calendarExpansionMax": 5,
"items": [
{
"id": "%my_email@gmail.com%"
}
]
}
var options = {
'method': "POST",
"payload": payload,
"muteHttpExceptions": true,
"payload": JSON.stringify(payload),
"contentType": 'application/json',
"timeZone": "CEST",
};
var result = UrlFetchApp.fetch(url, options);
Logger.log(result);
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.name);
Logger.log(params.blog);
} else {
Logger.log(result);
}
}