使用Google Apps脚本的Web App无法与分布式URL一起使用

程序的作用:我编写了一个简单的Google Apps脚本,该脚本可从当前登录的Gmail帐户中的标签中检索未读的电子邮件,并返回这些电子邮件的数量并显示在网站。

问题::当我从开发该计算机的计算机上创建一个Web应用程序并从网站上运行该应用程序时,此程序将运行。但是,如果我复制该URL并将其分发到以其他Google帐户身份登录的另一台计算机上,它将无法正常工作。

代码:

/*Sample.gs*/ 
function doGet(e){
    return HtmlService.createHtmlOutputFromFile('Run.html');
}

function GetUnreadCount(sourceLabel){
    return GmailApp.getUserLabelByName(sourceLabel).getUnreadCount();
}

function Main()
{   
    /*For testing,return unread email count from "CustomerA" label of Gmail*/
    return GetUnreadCount("CustomerA");
}

/*Run.html*/
<!DOCTYPE html> 
<html>
  <head>
    <base target="_top">
    <script>
        function CallGetUnreadCount() {

          document.getElementById('Messages').innerHTML = 'Counting...';

          google.script.run
            .withSuccessHandler(onSuccess)
            .withFailureHandler(onFailure)
            .Main();          
        }

        function onSuccess(UnreadCount) 
        {
          document.getElementById('Messages').innerHTML = "There are " + UnreadCount + " unread emails.";
        }

        function onFailure(error) 
        {
          document.getElementById('Messages').innerHTML = "Err: " + error.message;
        }

    </script>
  </head>
  <body>
    <div>
        <h1 id="Messages">Count # of unread emails</h1>
    </div>
    <div>
        <button type="button" onclick='CallGetUnreadCount();' id="CountButton">Count Unread Emails</button> 
    </div>
  </body>
</html>

如果我在编写此脚本的计算机上运行该程序,则该程序可以正常运行。但是,如果我将其部署为Web应用程序并分发URL(以/exec结尾),然后在以其他Gmail帐户登录的另一台计算机上运行,​​即使未读邮件也始终返回0。带有CustomerA标签的电子邮件。 令我感到困惑的是,如果我在另一台计算机上创建一个新项目并将上述代码复制粘贴到.gs文件和html文件中并作为Web部署,则该程序可以在另一台计算机上运行应用程式。

如果每台计算机都自行部署Web应用程序,为什么同一程序可以工作,但是如果我分发URL,则为什么该程序不能工作?

yuyunc1 回答:使用Google Apps脚本的Web App无法与分布式URL一起使用

当部署为Web应用程序(Publish > Deploy as web app)时,有一些配置选项:

deploy as web app

  • 执行方式:可以是 Me ,也可以是正在访问网络应用的用户。对于您的情况,您必须使用后者使它按预期工作。
  • 有权访问此应用的人:可以是只有我自己您域中的任何人任何人 。您必须根据需要确保它是第二个或第三个选项,以便用户可以正确执行它。
本文链接:https://www.f2er.com/3148411.html

大家都在问