如何将Postman的Intercom API请求转为可用的Google Apps Script代码?
修复Intercom API导出任务的Google Apps Script脚本
你的脚本无法运行主要是因为Google Apps Script的UrlFetchApp.fetch()参数格式不符合要求,以下是修复后的完整脚本,以及关键问题的说明:
修复后的代码
function exportIntercomContent() { // 可修改的时间参数(Unix时间戳) const createdAtAfter = 1654041600; const createdAtBefore = 1656547200; const url = 'https://api.intercom.io/export/content/data'; const options = { method: "post", // 注意:这里是小写的method,不是大写Method headers: { // 同样,headers是小写 "Authorization": "Bearer 123456789", "Accept": "application/json", "Content-Type": "application/json" }, payload: JSON.stringify({ // 请求体需要转为JSON字符串,用payload字段 "created_at_after": createdAtAfter, "created_at_before": createdAtBefore }), muteHttpExceptions: true // 开启后可以捕获HTTP错误,方便调试 }; try { const response = UrlFetchApp.fetch(url, options); const responseData = JSON.parse(response.getContentText()); console.log("请求成功,响应数据:", responseData); // 这里可以添加后续处理逻辑,比如把响应写入Google表格 } catch (error) { console.error("请求失败:", error.message); console.error("响应内容:", response.getContentText()); } }
关键修复点说明
- 参数键名大小写:
UrlFetchApp的配置对象中,方法名必须是小写的method,请求头字段是小写的headers,不能用大写的Method/Headers - 请求体格式:因为API要求
Content-Type: application/json,必须把JavaScript对象转为JSON字符串,并且用payload字段传递(不是你原来写的Body) - 错误处理:添加
try-catch块和muteHttpExceptions: true,可以在脚本出错时查看具体的错误信息和API返回的错误内容,方便调试 - 参数解耦:把时间戳提取为独立变量,后续修改时直接修改变量值即可,不用改动请求体的结构
内容的提问来源于stack exchange,提问作者Damien
相关产品推荐
相关产品推荐

