What the program does: I have written a simple Google Apps Script that retrieves unread emails from a label in the Gmail account currently logged in, and returns the number of those emails, and displays it on the website.
Problem: This program works when I created a web app from the computer I developed it on, and run it from the website. However, if I copy the URL and distribute it to another computer that is logged in as another Google account, it doesn't work.
Code:
/*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>
This program works fine if I run it on the computer I wrote this script on. However, if I deploy it as a web app, and distribute the URL (ending with /exec), and run it on another computer that has logged in as a different Gmail account, it always returns 0 even when there are unread emails in CustomerA label.
What confuses me is, this same program works on another computer if I create a new project on that computer and copy-paste the above code into a .gs file and an html file, and deploy as a web app.
Why does the same program work if each computer deploys the web app on their own, but not work if I distribute the URL?

each computerofif each computer deploys the web app on their own.each computermeans each Google account? - TanaikeExecute the app as:andWho has access to the app:? I thought that your issue might be resolved by the settings of Web Apps. - Tanaike