You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ImportJSONBasicAuth调用Investec银行API导入Google Sheets报400错误求助

你遇到的400报错核心是请求不符合Investec OAuth2授权接口的规则,具体错误点和解决方法如下:

错误原因

  • 请求方法错误:Investec的/identity/v2/oauth2/token接口仅接受POST请求,你最初使用的ImportJSONBasicAuth函数未指定请求方法,UrlFetchApp默认发送GET请求,不符合要求。
  • 缺少必填请求参数:OAuth2客户端凭证授权模式要求请求体必须携带grant_type=client_credentials参数,你两次调用都没有传递该参数,接口无法正常响应。
  • 参数传递错误:你合并后的ImportJSONBasicAuthViaPost函数调用时,没有传入payload参数对应的值,导致请求体为空,依然不符合接口要求。

解决方法

方案1:使用你合并后的函数调用

直接按如下格式填写参数即可,注意替换为你自己的Client ID和Client Secret:

=ImportJSONBasicAuthViaPost("https://openapi.investec.com/identity/v2/oauth2/token", "你的Client ID", "你的Client Secret", "grant_type=client_credentials", "", "", "")

方案2:修改原始ImportJSONBasicAuth函数

直接修改原函数代码,补充请求方法、必填请求体和内容类型,修改后代码如下:

function ImportJSONBasicAuth(url, username, password, query, parseOptions) {
  var encodedAuthInformation = Utilities.base64Encode(username + ":" + password);
  var options = {
    headers: {Authorization: "Basic " + encodedAuthInformation},
    method: "POST",
    payload: "grant_type=client_credentials",
    contentType: "application/x-www-form-urlencoded"
  };
  return ImportJSONAdvanced(url, options, query, parseOptions, includeXPath_, defaultTransform_);
}

修改完成后直接按你原来的调用方式调用即可。

排查技巧

如果修改后依然报错,可以在ImportJSONAdvanced函数第220行前添加异常静默参数,打印接口返回的具体错误信息:

function ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeFunc, transformFunc) {
  fetchOptions.muteHttpExceptions = true; // 新增这行
  var jsondata = UrlFetchApp.fetch(url, fetchOptions);  //line 220
  console.log(jsondata.getContentText()); // 新增这行,执行后在Apps脚本执行日志查看具体报错
  var object   = JSON.parse(jsondata.getContentText());
  
  return parseJSONObject_(object, query, parseOptions, includeFunc, transformFunc);
}

内容的提问来源于stack exchange,提问作者Ben Blaine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 05:30:03