Web App call fails with "... Error code INTERNAL" with Custom API
Asked Answered
O

1

1

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;
}

Option answered 23/5, 2020 at 3:59 Comment(12)
Does this answer your question? Find the ID of the attached spreadsheet from doGet functionMeteoric
Initially, I wanted to ask you a couple of questions, but I think you have an age-old problem - take a look at least at this question and the one mentioned above. I've flagged your question as duplicate (don't worry, it's just to point others). In short, the reason is getActiveSpreadsheet() - it is not available in web app context (although docs say that functions "can get a reference" with it, by "spreadsheet context" they mean "document-bound scripts" only).Meteoric
Just a suggestion - why don't you use the official Sheets API library since you use Node.js anyway?Meteoric
@OlegValter So if I am understanding this, I need to use SpreadsheetApp.openById instead of SpreadsheetApp.getActiveSpreadsheet. using this for the value of ss. However, doing this results in a different error . " Exception: Unexpected error while getting the method or property openById on object SpreadsheetApp."Option
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 APIMeteoric
If you did all the above (except for switching, ofc :) - check if the web app works under the /dev path (you most likely know that, just eliminating the most obvious) and update the deployed version if so. Please, do let me know if the issue persists - as you've noticed, error messages in GAS are not very helpful by themselvesMeteoric
@OlegValter The app is set to run as me, I have given permissions, and i have no problem starting the doGet function from the editor. I get the same error while running from the script editor manually. I am using the dev path for testing.Option
Thanks, that eliminated a lot of possible reasons. The usual culprit is permissions setup, but let's check a couple of things with the doc - what's the sharing policy on the document you are accessing and who is the owner [oh, and one more thing - is the scrip bound to the document or a standalone project]? Try a simple experiment - create a sample spreadsheet that's open to everyone and access it instead, if this fails then I would concider opening an issue on tracker (though, I'll try a similar setup myself to check for reproducibility)Meteoric
I own the sheet, there is no other manual shares, but anyone with the link can view(so I can show people I know). The script is bound to the sheet. I will make the experiment thing right now.Option
Thanks again - btw, I did a similar setup a couple of minutes ago and everything worked well, although mine is a standalone web app (creating a bound script does not scale), will check for bound scripts.Meteoric
@OlegValter Thank you for all your help, I made a few changes in some places, and everything seems to work on both ends. I will go ahead and mark the answer. Once again, thanks for taking your time to help me out! :)Option
Glad you worked it out - curious of what was the issue, though ( btw, there was a bug recently that affected some specific IDs (!), so sometimes these problems tend be fixed by themselves ). Sure, just mark that one - maybe we collectively save some poor soul a couple of hours of headscratch :)Meteoric
O
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

Option answered 23/5, 2020 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.