Use o Web App do Google Script como Webhook para receber diretamente a notificação por push
Meu objetivo: alterações no Google Drive => Enviar notificação parahttps://script.google.com/a/macros/my-domain/... => O aplicativo é pressionado para executar uma ação. Não quero configurar um agente Webhook intermediário para receber notificações. Em vez disso, deixe o Web App (pelo Google Script) recebê-lo e seja enviado diretamente.
Como a função relevante não é documentada (apenas aqui:https://developers.google.com/drive/web/push), abaixo está o código que tentei, mas falhei. 1. A ideia acima é viável? 2. Meu código doPost (R) parece que não pode receber a notificação (parâmetro R) corretamente. De qualquer forma, nenhuma resposta depois de alterar o Google Drive. Algum problema? (Tentei registrar o parâmetro de entrada R para ver sua estrutura real e decidir se o parâmetro Obj para OAuth é igual ao aplicativo de unidade normal, mas ocorre um erro antes do log)
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);
}
....