使用Google Apps脚本和推送通知的Gmail观看

时间:2016-09-20 06:20:45

标签: javascript google-apps-script gmail-api

我正在创建一个应用,使用Google Apps脚本为应用用户的Gmail帐户添加监视功能。

为了测试手表的工作,我使用了以下应用程序脚本代码,这是我从Spencer Easton(https://www.youtube.com/watch?v=wjHp9_NAEJo)的youtube教程中获得的

function doPost(e)
{
    var message = JSON.parse(e.postData.getDataAsString()).message;
    var data = Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString();
    var ss = SpreadsheetApp.openById('my_google_sheet_id').getSheets()[0];
    ss.appendRow([new Date(), message.message_id, data, "newMail"]);

    return 200;
}


function enrollEmail() {

    var watchRes = Gmail.newWatchRequest();

    watchRes.labelIds = ["INBOX"];
    watchRes.labelFilterAction = "include";
    watchRes.topicName = "projects/project-id-12345/topics/gmailTrigger";

    var response = Gmail.Users.watch(watchRes, "me");
}


function doGet()
{
    enrollEmail();
}

然后我使用以下步骤发布了应用程序:

1.  Deploy as web app
2.  Execute the app as: Me
3.  Who has access to the app: Anyone, even anonymous

现在手表对我的Gmail帐号工作正常。

但我的要求是在我的应用中观看所有经过身份验证的用户。

所以,我尝试使用以下步骤发布应用程序:

1.  Deploy as web app
2.  Execute the app as: User accessing the web app
3.  Who has access to the app: Anyone

现在,该应用会向访问该网络应用网址的用户请求身份验证。但该手表不适用于用户的Gmail帐户和我的Gmail帐户。

我再次使用第一种方法发布应用程序(将应用程序作为:我执行),现在该手表适用于经过身份验证的用户帐户和我的帐户。

如何配置Google Apps脚本,以便我可以为对我的应用进行身份验证的用户添加监视。

2 个答案:

答案 0 :(得分:0)

由于您已经<iframe src="http://docs.google.com/gview?url=www.ezconf.net/docs/SOLI%202008/sample.doc&embedded=true" style="width:600px; height:500px;" frameborder="0"></iframe> 了一个主题,接下来要做的就是设置如何接收通知(设置webhooks)。

关于你的案子的好处是Spencer Easton的github有Gmail API with Push Notification的条目。

答案 1 :(得分:0)

我遇到了同样的问题。

你试过吗

return HtmlService.createHtmlOutput("");

而不是

return 200;
在doPlot函数中

也许在Spencer Easton编写代码时,Apps脚本仍然允许return 200;,但现在doPlot应该严格返回HtmlOutput对象