【问题标题】:Gmail watch using Google Apps Script and Push Notification使用 Google Apps 脚本和推送通知的 Gmail 手表
【发布时间】:2017-01-27 23:02:54
【问题描述】:

我正在创建一个应用程序,以便使用 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

现在,该应用要求对访问 Web 应用 URL 的用户进行身份验证。但手表不适用于用户的 gmail 帐户和我的 gmail 帐户。

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

如何配置 Google Apps 脚本,以便我可以为验证我的应用的用户添加手表。

【问题讨论】:

  • "但手表不适用于用户的 gmail 帐户和我的 gmail 帐户。"你能扩展一下吗?究竟是什么不起作用?
  • 将新电子邮件添加到收件箱时不会调用 doPost()。 (用户帐户和我的帐户)

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


【解决方案1】:

既然你已经watch一个话题,接下来要做的就是设置如何接收通知(设置webhooks)。

关于您的案例的好处是,Spencer Easton 的 github 有一个 Gmail API with Push Notification 的条目。

【讨论】:

  • 我也做过同样的事情。我正在为我的帐户获取 Google 表格中的日志。
【解决方案2】:

我遇到了同样的问题。

你试过了吗

return HtmlService.createHtmlOutput("");

而不是

return 200;

在doPlot函数中?

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

【讨论】:

  • video OP linked 中提到了此特性 - 这是确认 webhook 所必需的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 2020-02-06
  • 1970-01-01
  • 2022-09-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多