使用Google Script的Web App作为Webhook直接接收推送通知

Pat*_*Pat 5 webhooks push-notification google-apps-script google-drive-api

我的目标: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云端硬盘后没有回复.任何问题?(我已尝试记录输入参数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);  
  }


....
Run Code Online (Sandbox Code Playgroud)

Wee*_*oey 1

蝉,

我们已经多次完成类似的功能来接收 webhooks/API 调用。笔记:

  1. 要获得 R,您需要:var Response = R.parameters然后您可以执行Response.kindResponse.id等操作。

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