我使用的是第三方API,当我从浏览器中的API文档“测试”页面或Postman Windows本机应用程序发送邮件时,我的POST请求可以正常工作。
当我从Google Apps脚本发送相同的请求时,收到错误代码。我可以使用GAS发送/接收GET请求,也可以使用GAS向其他API发送/接收POST请求。
此GAS GET请求正确运行:
var options = {
"headers" : {
"Authorization" : "Bearer " + accessToken,
"Accept": "application/json"
}
};
var response = UrlFetchApp.fetch(url, options);
标题:
Accept: application/json
Accept-Encoding: gzip
Accept-Language: en-US
Authorization: Bearer {token}
Content-Length: 638
Content-Type: application/json
DNT: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML
X-Forwarded-For: xxx.xxx.xxx.xx
X-Forwarded-Port: 443
X-Forwarded-Proto: https
此GAS POST请求产生错误:
var options = {
"headers" : {
"Authorization" : "Bearer " + accessToken,
"Accept": "application/json",
"Content-Type": "application/json",
"Accept-Language": "en-US"
},
"method" : "post",
"payload" : JSON.stringify(this_payload)
};
var response = UrlFetchApp.fetch(url, options);
GAS POST请求发送以下标头:
Accept: application/json
Accept-Encoding: gzip,deflate,br
Accept-Language: en-US
Authorization: Bearer {token}
Connection: close
Content-Length: 387
Content-Type: application/json
User-Agent: Mozilla/5.0 (compatible; Google-Apps-Script)
此GAS POST请求给出错误:
Response Code 500
code: infrastructure.common.SystemException
description:"System error, please re-try"
API是否可能根本不接受来自GAS的连接?用户代理列出了Google应用脚本。还是我的某些参数错误了?我可以通过另外两个平台获得正确的参数,所以我认为参数是正确的。