Warm tip: This article is reproduced from serverfault.com, please click

Call and Run doPost

发布于 2020-11-27 17:51:35

Is it possible to call a doPost that exist in the same project. When I call the doPost in the same project I get a 200 response code but if I get someone else to run the same function they get a 500 response code. I am using the dev URL, executing the app as me, and anyone in my domain can access the app. Here is my sample file.

function sendPost(){
  var sheetURL = SpreadsheetApp.getActiveSpreadsheet().getUrl();
  
  var webAppUrl = "<<my dev url>>"; //insert webapp URL of Dev or Exe script file
  
  var auth = ScriptApp.getOAuthToken();
  var header = { 'Authorization': 'Bearer ' +  auth};
  var payload = {scriptName : 'updateDataV2', sheetURL : sheetURL};
  var options = { 
    method : 'post',
    headers : header,
    muteHttpExceptions : true,
    payload : payload
  };
  
  var resp = UrlFetchApp.fetch(webAppUrl, options);
  Logger.log(resp.getResponseCode());
  
  var message = resp.getResponseCode() == 200 ? 'Database update complete' : 'There seems to be an issue. Please try again.';
  //if(resp.getResponseCode() == 200){resetFormulas();}
  return SpreadsheetApp.getActiveSpreadsheet().toast(message);
  
//Below 2 lines needed to setup access
//DriveApp.getFiles();
//DriveApp.createFile(blob);
}

function doPost(e){
  if(!e.parameters.scriptName){return ContentService.createTextOutput('A function name must be passed in a payload to run.');};
  
  Logger.log('parameters from caller ' + JSON.stringify(e));
  
  var newText = this[e.parameters.scriptName](e); //This line runs the function being called with parameters
  
  return ContentService.createTextOutput(newText);
}

Here is the error message for the users.

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta name="description" content="Web word processing, presentations and spreadsheets">
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
      <link rel="shortcut icon" href="//docs.google.com/favicon.ico">
      <title>Error</title>
      <meta name="referrer" content="origin">
      <link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css">
      <style>/* Copyright 2020 Google Inc. All Rights Reserved. */
         .goog-inline-block{position:relative;display:-moz-inline-box;display:inline-block}* html .goog-inline-block{display:inline}*:first-child+html .goog-inline-block{display:inline}#drive-logo{margin:18px 0;position:absolute;white-space:nowrap}.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_116x41dp.png');background-size:116px 41px;display:inline-block;height:41px;vertical-align:bottom;width:116px}.docs-drivelogo-text{color:#000;display:inline-block;opacity:0.54;text-decoration:none;font-family:'Product Sans',Arial,Helvetica,sans-serif;font-size:32px;text-rendering:optimizeLegibility;position:relative;top:-6px;left:-7px;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:144dpi){.docs-drivelogo-img{background-image:url('//ssl.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_116x41dp.png')}}
      </style>
      <style type="text/css">body {background-color: #fff; font-family: Arial,sans-serif; font-size: 13px; margin: 0; padding: 0;}a, a:link, a:visited {color: #112ABB;}</style>
      <style type="text/css">.errorMessage {font-size: 12pt; font-weight: bold; line-height: 150%;}</style>
   </head>
   <body>
      <div id="outerContainer">
         <div id="innerContainer">
            <div style="position: absolute; top: -80px;">
               <div id="drive-logo"><a href="/"><span class="docs-drivelogo-img" title="Google logo"></span><span class="docs-drivelogo-text">&nbsp;Drive</span></a></div>
            </div>
            <p style="padding-top: 15px">Google Docs encountered an error. Please try reloading this page, or coming back to it in a few minutes.</p>
            <p>To learn more about the Google Docs editors, please visit our <a href="https://support.google.com/docs/?hl=en&p=error_help" target="_blank">help center</a>.</p>
            <p><br><b>We're sorry for the inconvenience.</b><br><i>- The Google Docs Team</i></p>
         </div>
      </div>
   </body>
   <style>html {height: 100%; overflow: auto;}body {height: 100%; overflow: auto;}#outerContainer {margin: auto; max-width: 750px;}#innerContainer {margin-bottom: 20px; margin-left: 40px; margin-right: 40px; margin-top: 80px; position: relative;}</style>
</html>
Questioner
wjwelch1
Viewed
0
contributorpw 2020-11-30 23:44:04

OK.

I debugged your code. And I saw the next line.

var newText = this[e.parameters.scriptName](e);

It's very interesting. And also it may get point to different things while you call a web app by different users.

Don't use this in that cases. Try override this to a dictionary or IIFE. In short you should be sure that all the code that your function wants to run has already been initialized. This will not work in the current context.

I copied the file then commented the next line

// var newText = this[e.parameters.scriptName](e);

then I re-published the app and it works fine now.

Additional

You need to publish the app for all auth users

enter image description here

or for all anonymous

enter image description here