0

I have a Google Apps Script Web App embedded in a Google Sheet (meaning, the Web App code is built into a spreadsheet, and a UI in the spreadsheet is triggering the functionality of the Web App with the UrlFetchApp) through a function that looks like the following:

function cBack(strObj) {
    var url = ScriptApp.getService().getUrl();
    var options = {
        'method' : 'post',
        'payload' : JSON.parse(strObj)
    };

    var response = UrlFetchApp.fetch(url, options);
.....

When the function is run with valid data passed in, I get this error:

Request failed for https://docs.google.com returned code 404

The full response appears to be:

<!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>Page Not Found</title><meta name="referrer" content="origin"><link href="//fonts.googleapis.com/css?family=Product+Sans" rel="stylesheet" type="text/css" nonce="caJsSUva3OGNA7SlHa/0Xg"><style nonce="caJsSUva3OGNA7SlHa/0Xg">/* Copyright 2021 Google Inc. All Rights Reserved. */

And the URL that comes through with the url variable is https://docs.google.com/macros/s/xxxxxxxxxxxxxxxxxxxxxxxxx/exec

I have run this in incognito mode so there is only one account signed in. I have also tried deploying the web app for only my viewing and for anyone to access. All have given me the same results.

Why do I keep getting this error when it is run? This has worked in the past, seems to just be not working in a new deployment of the web app now...

Lle.4
  • 516
  • 4
  • 17
  • What do you mean by "I have a Google Apps Script Web App embedded in a Google Sheet "? Also please add a [mcve]. – Rubén Jan 15 '22 at 22:49
  • Thanks for the reply! The Web App code is built into a spreadsheet, and a UI in the spreadsheet is triggering the functionality of the Web App. Unfortunately, due to the complexity of this process, a minimal reproducible example will be all but impossible. I'm hoping someone with web app experience will be familiar enough with the syntax I provided to resolve the issue! – Lle.4 Jan 15 '22 at 23:01
  • By definition a [mcve] doesn't reproduce the whole process, it should reproduce only the behaviour of the part that is having problems. Anyway, please [edit] the question to include the clarification directly in the question also include the web-app settings and if your tried it using Chrome in incognito mode having sign-in on only one account. – Rubén Jan 15 '22 at 23:08
  • Thanks for the feedback! I have edited the question accordingly. Really appreciate it, and having such a supportive community here! – Lle.4 Jan 15 '22 at 23:12
  • 2
    Can you provide your script of Web Apps side? – Tanaike Jan 16 '22 at 00:38

1 Answers1

0

You haven't mentioned that have tried to set the web app to be run by anyone even anonymous so it's very likely that the problem is that UrlFetch call is missing the authentication parameters.

Instead of

var options = {
        'method' : 'post',
        'payload' : JSON.parse(strObj)
    };

try

var token = ScriptApp.getOAuthToken();
var options = {
        'method' : 'post',
        'headers': {'Authorization': 'Bearer '+  token},
        'payload' : JSON.parse(strObj)
    };

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thank you so much for the suggestion! Unfortunately, this doesn't seem to have worked :( I'm still getting the same error, not entirely sure why. – Lle.4 Jan 16 '22 at 01:40
  • 1
    @Lle.4 As suggested previously make a [mcve] and include it in the question. – Rubén Jan 16 '22 at 02:03