Google 应用程序脚本抛出 BAD 请求,Postman 和 Curl 正常,甚至浏览器回调

Google App Script throw BAD request , Postman and Curl OK, even browser callback

我正在尝试 运行 下面的代码在 VPN 中的 Google Gitlab 应用程序脚本上。

VPN 已连接,我可以在 Curl 命令、浏览器和 Postman 上收到响应,但 Google 应用程序脚本出错。

this._url = "https://<url access on vpn and vpn is on>/api/v4/projects/1048/repository/compare?to=release%2F1.209.0&from=release%2F1.208.0"; this.payloadData = null;

this.fetchArgs = {   contentType: "application/json",   headers: { "Authorization": "Bearer <mytoken-accesible on Postmand and Curl>" },  muteHttpExceptions: true,   method: methodType, }

try {   this.responseData = UrlFetchApp.fetch(this._url, this.fetchArgs);   this.responseCode = this.responseData.getResponseCode(); } catch (e) {   console.log(e); }

响应总是如下

{ [Exception: Bad request: https://<baseURL>/api/v4/projects/1048/repository/compare?to=release%2F1.209.0&from=release%2F1.208.0] name: 'Exception' }

如果您需要 VPN 来访问 link,并且您可以使用 Postman、curl 和浏览器访问它,但应用程序脚本不能访问它,那是因为所有这 3 个访问 URL 在本地,因此他们可以使用 VPN 访问它。

另一方面,Apps 脚本会远程执行此操作(它们 运行 在 google 服务器上),因此,您在没有任何 VPN 的情况下访问 URL,导致错误请求问题。

据说您可以使用 SDC 来解决这些问题,但它已经被弃用了。

参考:

  • Apps Script, SQL DB, and VPN
  • Does code on a Google sheet runs locally or on a Google server? - UrlFetchApp returns empty