【问题标题】:Use Google Script's Web App as Webhook to receive Push Notification directly使用 Google Script 的 Web App 作为 Webhook 直接接收推送通知
【发布时间】:2014-08-01 15:39:34
【问题描述】:

我的目标:Google 云端硬盘的更改 => 将通知推送到 https://script.google.com/a/macros/my-domain/... => 应用被推送以采取行动。 我不想设置中间 Webhook 代理来接收通知。相反,让 Web App(通过 Google Script)接收并直接推送。

由于相关功能没有记录(就在这里:https://developers.google.com/drive/web/push),下面是我尝试但失败的代码。 1.以上想法可行吗? 2. 我的代码 doPost(R) 似乎无法正确接收通知(R 参数)。无论如何,我更改 Google Drive 后没有任何反应。任何问题? (我已经尝试记录输入参数R,以查看其真实结构,并确定OAuth的参数Obj是否与普通Drive App相同,但在记录之前出现错误)

function SetWatchByOnce(){
  var Channel = {
    'address': 'https://script.google.com/a/macros/my-domain/.../exec',
    'type': 'web_hook',
    'id': 'my-UUID'
  };

  var Result = Drive.Changes.watch(Channel); 
  ...
}    

function doPost(R) {
  var SysEmail = "My Email";
  MailApp.sendEmail(SysEmail, 'Testing ', 'Successfully to received Push Notification');

  var Response = JSON.parse(R.parameters);
  if (Response.kind == "drive#add") {
    var FileId = Response.fileId;
    MyFile = DriveApp.getFolderById(FileId);
    ...
  }
}


function doGet(e) {
  var HTMLToOutput;  
  var SysEmail = "My Email";

  if (e.parameters.kind) { 
      //I think this part is not needed, since Push Notification by Drive is via Post, not Get. I should use onPost() to receive it. Right?

    } else if (e.parameters.code) { 
      getAndStoreAccessToken(e.parameters.code);
      HTMLToOutput = '<html><h1>App is successfully installed.</h1></html>';

    } else { //we are starting from scratch or resetting
      HTMLToOutput = "<html><h1>Install this App now...!</h1><a href='" + getURLForAuthorization() + "'>click here to start</a></html>";
    }  

    return HtmlService.createHtmlOutput(HTMLToOutput);  
  }


....

【问题讨论】:

  • 开发 Google Apps 脚本是如此孤独和无助。有人可以帮忙回答我的问题吗?
  • 我很遗憾没有遵循 Weehooey 之前的评论并通过 fetch 进行测试(我失败了)。我认为它与我在浏览器中输入 url 的功能相同(我成功)。有人说如果应用程序以域用户身份运行,Google Apps 会接受传入的提取。所以我尝试以开发者身份运行 App,甚至在个人 Gmail 帐户中运行 App,但仍然失败。 UrlFetchApp.fetch(REDIRECT_URL, {"followRedirects" : true, ...});现在我承认问题来自:doGet() & doPost() > Drive.changes.watch > 使用 Google Script 的 Url 作为 Web_hook 的可行性。
  • 从哪里可以得到 UUID?
  • 频道 URL 是什么...是我们服务器的 URL 吗?

标签: google-apps-script push-notification webhooks google-drive-api


【解决方案1】:

Cloud FunctionsHTTP trigger(s) 也可能是一个选项...

(在提出这个问题时还不存在)。这只需要在 Google Drive 设置中将触发器 URL 设置为通知 URL - 并为触发器添加一些 NodeJS 代码;不管它做什么。一个可以,例如。发送电子邮件和/或FCM 类似的推送通知。该触发器也可以从应用脚本触发,UrlFetchAppApp Script API。一个触发器可以有多个触发器,它们执行不同的任务(App Script 只是一种可能性)。

【讨论】:

    【解决方案2】:

    蝉,

    我们已经多次完成类似的功能来接收 webhook/API 调用。备注:

    1. 要获得R,你需要:var Response = R.parameters,然后你可以做Response.kindResponse.id等。

    2. Logger 不能与 doGet() 和 doPost() 一起使用。我将它设置为写入电子表格 - 在任何严肃的代码之前。这样我就知道它是否被触发了。

    【讨论】:

    • 谢谢 Weehooey!本周我将对您的评论进行审判。
    • 嗨 Wee​​hooey,我已经在帖子中修改了我的代码。请看一下。 1. 修改 doPost() 2. 显示其他我的应用程序供您检查。但它们只是一些标准的 doGet() 和授权代码。如您所见,我将其添加到 doPost() 内的第一条语句中以跳过任何可能的错误。证明它从来没有被推过! MailApp.sendEmail(SysEmail, 'Testing', '成功接收推送通知');
    • 备注:在 Google Developers Console/APIS & AUTH/Push 中,我添加了 3 个域: 1.script.google.com a) 文档显示需要带有 https 的域。由于我没有 https,这也是使用 script.google.com/a/macros/power-origin.com/s/.../exec 作为 webhook 的另一个原因。 b)但它不允许我添加整个域,而是 script.google.com。不知道会不会影响结果。 2.my-domain a) 不是 https,但 Google 接受我的输入。它由 Google 协作平台提供。
    • Cicada,您可能需要添加另一个域:script.googleusercontent.com - 这是回复的来源。此外,如果您使用浏览器点击script.google.com/a/macros/power-origin.com/s/.../exec 并附加: ?test=testing 并查找 R.parameters.test ,它应该为您提供值“testing”。还要记住创建和发布每个新的项目版本(或使用 /dev 而不是 /exec)。
    • 嗨 Wee​​hooey, 1. 每次修改后我都会更新发布。 2.“script.googleusercontent.com”不允许添加到域选项中。 3.通过使用?test=testing等,我可以测试doGet(),没有问题。但据我所知,通知是由 Drive.Changes.watch “发布”的,所以我认为我需要使用 doPost() 来接收通知。但是无论是doGet()还是doPost(),在我对Drive进行更改后,他们都没有收到任何推送的更改通知。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多