Google Apps Script调用Wise API返回数据异常问题排查
问题诊断与修复方案
核心错误分析
你的Google Apps Script请求存在3个关键问题,导致API返回不符合预期的汇率:
- Payload格式完全错误:
JSON.stringify的对象中,给键名和值额外套了双引号(比如"\"sourceCurrency\"":"\"EUR\""),这会让API无法解析正确的请求参数。 - 参数类型错误:
sourceAmount被设为字符串"null",但API要求的是原生null值,这会导致金额计算逻辑偏离预期。 - 授权头拼接错误:
"Authorization": "Bearer + mytoken"中的+是多余的,正确格式应为"Bearer 你的令牌"。
修正后的代码
const url = "https://api.transferwise.com/v3/quotes/"; const mytoken = "你的实际令牌"; // 替换为你的有效Token const response = UrlFetchApp.fetch(url, { method: "POST", headers: { "Authorization": "Bearer " + mytoken, "Content-Type": "application/json" }, muteHttpExceptions: true, contentType: "application/json", payload: JSON.stringify({ sourceCurrency: "EUR", targetCurrency: "USD", sourceAmount: null, targetAmount: 1000 }) }); const data = JSON.parse(response.getContentText()); Logger.log("Response code is %s", response.getResponseCode()); Logger.log(data.rate);
验证说明
修正后,请求的Payload会和Postman中的格式完全一致:
{ "sourceCurrency": "EUR", "targetCurrency": "USD", "sourceAmount": null, "targetAmount": 1000 }
API会正确识别你需要计算「目标金额1000 USD对应的EUR金额」逻辑,返回的汇率结果将与Postman一致。
内容的提问来源于stack exchange,提问作者WILLIAM PERBONI
相关产品推荐
相关产品推荐

