问题描述
在 Google Apps 脚本中,我通过 HTTPS 请求调用我的 Cloud Function,然后调用 API,并转发响应。我这样做是因为 API 需要一个静态 IP 地址。我收到一条成功的 Status:200 消息从外部 API 返回到 Cloud Functions。然后我尝试将响应转发回 GAS 并在 GAS 中获取状态:502。
我认为 Status:502 是内存错误?此函数适用于较小的响应。
我的 Google Apps 脚本函数:
function gas_function(){
var token = ScriptApp.getIdentityToken();
var params = {muteHttpExceptions: true,headers: { Authorization: 'Bearer ' + token } };
var url = 'https://'+REGION+'-'+PROJECT_ID+'.cloudfunctions.net/'+RECEIVING_FUNCTION;
var response = UrlFetchApp.fetch(url,params);
// I should be getting my 'Status:200' response from the Cloud Function with my data
// This works with small datasets,but with a large dataset I get
// Response Code = 502 Error: Could not handle the request
return response;
}
我的谷歌云函数:
exports.cloud_function = functions.https.onRequest(async (request,response) => {
var url = 'https://externalapi.com/info/';
var api_response = await axios.get(url);
console.log(api_response.data); // this works! I have a response with ~12000 data records
// Now I just need to send it back to Google Apps Script
response.status(200).send(api_response.data);
});
解决方法
如果 UrlFetchApp.fetch()
收到大于 GAS 限制 (50MB) 的响应,您将收到 Status:502 响应。
API 方面的解决方案:
- 对您的 Cloud Function 响应进行分页以使其更小
- 直接从 Cloud Function 执行 GAS 操作
请参阅评论以获取配额和限制的链接 - 谢谢