简体   繁体   English

对已部署 WebApp 的 Google Apps 脚本 URL 请求产生 404 错误

[英]Request to Google Apps Script URL for deployed WebApp produces 404 error

This issue is very similar to others (like Google Drive Page Not Found - Sorry, unable to open the file at this time for example) posted here.此问题与此处发布的其他问题非常相似(例如Google Drive Page Not Found - 抱歉,此时无法打开文件)。 It's not exactly the same, but I do believe it has the same root issue illustrated in that post in that trying to submit a form to a Google App Script while logged into more than to 1 Google account causes /u/1 and/or /u/0 to be added to the script's URL thus producing a 404 Error.它不完全相同,但我确实相信它与该帖子中说明的根本问题相同,即在登录多个 Google 帐户时尝试向 Google App 脚本提交表单会导致/u/1和/或/u/0添加到脚本的 URL 从而产生 404 错误。

This is using a standard Google account - not G-Suite.这是使用标准的 Google 帐户,而不是 G-Suite。

I have a form on a website that submits to a Google Apps Script via AJAX.我在一个网站上有一个表单,它通过 AJAX 提交给 Google Apps 脚本。 The script makes some API calls to create a Google Doc containing the data collected by the form.该脚本进行一些 API 调用以创建包含表单收集的数据的 Google Doc。

HTML/Javascript: HTML/Javascript:

<form>
  <input type="text" name="full_name">
  <input type="text" name="phone">
  <input type="submit">
</form>

$('form').submit(function() {
  var obj = $(this).serializeObject();
  var gurl = "https://script.google.com/macros/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec";

  $.ajax({
    url: gurl,
    type: "GET",
    data: obj,
    dataType: "jsonp",
    success: function(data, status, xhr) {
      console.log("success");
      console.log(data);
    });
});

GoogleScripts谷歌脚本

function doGet(e) {
  var params = e.parameters
  var result = {};
  try {
    result = {
      status: start(params),
      msg: 'Success',
      vals: formData,
      rawVals: params,
      errs: errors
    }
  } catch (f) {
    result.error = f.toString();
 }
 return ContentService     
  .createTextOutput(e.parameters.callback + '(' + JSON.stringify(result) + ')')
  .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

Submitting the form while logged into more than 1 Google account in the same browser results in the following error in the console and the form does nothing:在同一浏览器中登录多个 Google 帐户时提交表单会导致控制台中出现以下错误,并且表单什么也不做:

jquery.js?ver=1.12.4-wp:4 GET https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec?callback=jQuery112407830193282901534_1608623376571&s&full_name=Dave+Pe&phone=1111111111_=1608623376572 net::ERR_ABORTED 404 jquery.js?ver=1.12.4-wp:4 GET https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec?callback=jQuery112407830193282901534_1608623376571&s&full_name=Dave+Pe&phone=1111111111_= 1608623376572净::ERR_ABORTED 404

When I go to Network tab to view the request, the Header tab there shows the following:当我 go 到 Network 选项卡查看请求时,Header 选项卡显示以下内容:

Request URL: https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec?callback=jQuery112407830193282901534_1608623376571&s&full_name=Dave+Pe&phone=1111111111_=1608623376572

Notice the /u/1/ that have been inserted into the URL that are not present in the URL I pass to my $.ajax() call.请注意已插入到 URL 中的 URL 中不存在的/u/1/我传递给我的$.ajax()调用。

Most of the answers I've found for this issue say to just remove the /u/1/ , but since I didn't add it in the 1st place, I don't know where I would remove it from.我为这个问题找到的大多数答案都说只是删除/u/1/ ,但由于我没有将它添加到第一个位置,所以我不知道从哪里删除它。

Can anyone confirm that this seemingly known issue (of having the URL altered when logged into multiple Google accounts) is what is causing my problems?任何人都可以确认这个看似已知的问题(在登录多个 Google 帐户时更改 URL)是导致我的问题的原因吗? Any ideas as to how I can go about making my request to:关于如何向 go 提出我的请求的任何想法:

https://script.google.com/macros/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec

and not并不是

https://script.google.com/macros/u/1/s/AKfycbzmhaub3ojPARA-B-Y2uVC2BJZPaRvbgMwMTH9pd7R9aHuAD5M/exec

?? ?? or is there something more deeply wrong with the way I'm trying to use Google Scripts here?还是我在这里尝试使用 Google Scripts 的方式有更严重的错误?

My solution was to move the request from the client side to the server.我的解决方案是将请求从客户端移动到服务器。 I submit my form values to a server-side page via AJAX and then on that page, I make an HTTP request with cURL to my Google Apps Script (sending the form data in the body) and send the response back to the client.我通过 AJAX 将表单值提交到服务器端页面,然后在该页面上,我使用 cURL 发出 HTTP 请求,并将响应发送到我的 Google Apps 脚本(将表单数据发送回给我的正文)。

Seems to be working... no issues I can think of but that doesn't mean they don't exist.似乎正在工作......我没有想到任何问题,但这并不意味着它们不存在。 If there are any holes to be shot in that approach, please feel free to unload.如果该方法有任何要射击的孔,请随时卸载。

Watered down...冲淡...

JavaScript JavaScript

$.ajax({
    url: '/ajax-handler.php',
    type: "POST",
    data: obj
    success: function(data, status, xhr) {
      console.log("success");
      console.log(data);
    });
});

PHP PHP

$vals = my_sanitize_func($_POST);

$url = GAS_URL;
$fields = $vals;

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

// handle/format response;
$result = ...

print $result;

You can also do whatever you want this way.你也可以用这种方式做任何你想做的事情。
Google App Script (Code.gs):谷歌应用脚本(Code.gs):

function doGet(e){
     var action = e.parameter.action;
     if (action=="sendSuccess"){
        justSendSuccess(e);
      }
   }

   function justSendSuccess(e){
   var output = JSON.stringify({"result":"success"});
        return ContentService
          .createTextOutput(e.parameter.callback+"("+ output + ");")
          .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

JavaScript Part: JavaScript 零件:

function callGoogleScript(){
        console.log("Started");
        var url = "https://script.google.com/macros/s/###SCRIPT_ID###/exec";
        $.ajax({
        crossDomain: true,
        url: url,
        data: {
        "action":"setInfo"
        },
        method: "GET",
        dataType: 'jsonp',
        jsonp: "callback",
        success: function(data){
        console.log("SUCCESS!");
        console.log(data);
  }
});

    }

I tried to run it (authenticated with a single account) and it seems to work.我尝试运行它(使用单个帐户进行身份验证),它似乎工作。 This error seems to happen because you are authenticated with multiple accounts.发生此错误似乎是因为您使用了多个帐户进行了身份验证。 Also, it seems as it has already been documented at Google Issue Tracker ( link to issue ).此外,它似乎已经记录在 Google 问题跟踪器(问题链接)中。 If you want it to make it more visible, you can click the white star (☆) which tells google that you are affected by this issue.如果你想让它更显眼,你可以点击白星 (☆),它告诉谷歌你受到了这个问题的影响。

As a side note, notice that the code you make will be executed by everyone as you.作为旁注,请注意您编写的代码将由每个人执行。 This code will have your privileges.此代码将拥有您的权限。 Be very careful.要非常小心。 Your account limits may also apply.您的帐户限制也可能适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM