Web App call fails with “… Error code INTERNAL” with Custom API

本小妞迷上赌 提交于 2021-01-01 07:45:27

问题


I have a spreadsheet with a collection of cards, and I wrote a doGet() function to pick a random one, and return it to the user. When entering the Web App url into a web browser, the result appears fine. However, when I try to make a HTTP Get request through the request module, it returns this:

<!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">We&#39;re sorry, a server error occurred while reading from storage. Error code INTERNAL.</div></body></html>

Extracted error message is:

We're sorry, a server error occurred while reading from storage. Error code INTERNAL.

I googled this error code, but nobody's had the same ending of "Error code INTERNAL", and most were about other users. Here is my doGet function and request function

Google Apps Script:

function doGet(request){
  var requestType = request.parameter.type
  if(requestType == "random_card"){
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheets()[0];
    var entriesL = sheet.getLastRow() - 1;
    var cardNum = getRandomInt(entriesL);
    var card = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 2).getValue();
    var set = SpreadsheetApp.getActiveSheet().getRange(2 + cardNum, 1).getValue();
    var output = '{"card":"' + card + '","cardSet":"' + set + '"}';
    Logger.log(output)
    return ContentService.createTextOutput(output);
  }
}

Node.js

function randomCard(){
  var info;
  request.get('{url here}?action=get&type=random_card',function(err,res,body){
    console.log(body)
    info = JSON.parse(body);
  });

  return info;
}


回答1:


Yep, this should be enough. The error is indicative of the permission error (since you call the webapp externally - although you should've got the permission before as the requirement is the same with getActiveSpreadsheet) - try opening the web app in script editor, run the function in question and authorize it. If you already did so, check if the web app is configured to run "as me" (if you are ok with that) or consider switching to the Sheets API – Oleg Valter



来源:https://stackoverflow.com/questions/61967246/web-app-call-fails-with-error-code-internal-with-custom-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!