【发布时间】: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