Google AppsScript调用Telzio API返回403错误,Postman可正常请求
问题排查与修复
核心问题:请求格式不匹配
你的代码存在Content-Type与请求体格式冲突的问题:
- 你设置了
contentType: 'application/x-www-form-urlencoded',但却将payload序列化成了JSON字符串,这会导致API无法正确解析请求内容,进而返回403错误(API可能将格式非法判定为权限问题)。 - Postman能成功,是因为它自动匹配了正确的请求格式(比如你在Postman里可能用了JSON格式且设置了对应Content-Type)。
修复方案
根据Telzio v3 API文档,该接口接受JSON格式请求体,调整请求配置即可:
修正后的代码
function sendScheduledSMS() { const token = '[my_token]'; // 替换为你的实际token const number_id = '[number_id]'; const recipient = '[recipient_number]'; const text = 'test'; const url = 'https://api.telzio.com/v3/conversations/sms/create-conversation'; const payload = { 'number_id': number_id, 'recipient': recipient, 'text': text }; const options = { 'method': 'post', 'contentType': 'application/json', // 改为JSON格式的Content-Type 'payload': JSON.stringify(payload), 'headers': { 'Authorization': 'Bearer ' + token }, 'muteHttpExceptions': true // 开启后可查看完整错误响应,便于排查 }; try { const response = UrlFetchApp.fetch(url, options); Logger.log('响应状态码: ' + response.getResponseCode()); const result = JSON.parse(response.getContentText()); Logger.log('SMS发送成功: ' + JSON.stringify(result)); } catch (error) { Logger.log('发送错误: ' + error); } }
额外排查点
- IP白名单限制:检查Telzio后台是否设置了IP白名单,Google AppsScript的请求IP为动态,若有白名单限制,建议暂时关闭测试。
- Token有效性:确认代码中的
token与Postman使用的完全一致,无多余空格或特殊字符。 - 参数格式验证:确保
number_id是Telzio平台内的有效号码ID,recipient为符合要求的格式(如带国家区号的E.164格式)。
内容的提问来源于stack exchange,提问作者ayabbear
相关产品推荐
相关产品推荐

