7

My Goal: Changes in Google Drive => Push Notification to https://script.google.com/a/macros/my-domain/... => App is pushed to take action. I don't want to setup an middle Webhook agent for receiving notification. Instead, let the Web App (by Google Script) to receive it and be pushed directly.

Since the relevant function is quite undocumented (just here: https://developers.google.com/drive/web/push) , below is the code I tried but failure. 1. Is above idea feasible?? 2. My code doPost(R) seems cannot receive notification (R parameter) properly. Anyway, no response after I change the Google Drive. Any problem? (I have tried to log the input parameter R so as to see its real structure and decide if the parameter Obj for OAuth is the same as normal Drive App, but error occur before 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);  
  }


....
Pat
  • 105
  • 1
  • 7
  • 1
    Developing Google Apps Script is so lonely and helpless. Could anybody please help to answer my question? – Pat Aug 04 '14 at 09:34
  • I feel regret that I haven't followed one of Weehooey's previous comment and test by fetch (I failure). I presume it is the same function as I type the url into browser (I success). Some people said Google Apps accept incoming fetch if App is running as Domain User. So I tried to run App as Developer and even run App in personal Gmail account, but still failure. UrlFetchApp.fetch(REDIRECT_URL, {"followRedirects" : true, ...}); Now I admit the problems come from: doGet() & doPost() > Drive.changes.watch > Feasibility in using Google Script's Url as Web_hook. – Pat Aug 08 '14 at 08:28
  • From where can i get UUID? – Ketav Sep 05 '17 at 10:21
  • And What is the Channel URL...Is it our server's URL? – Ketav Sep 05 '17 at 10:22

2 Answers2

2

Cloud Functions HTTP trigger(s) might also be an option ...

(which not yet existed at time of this question). this just requires setting the trigger URL as the notification URL, in the Google Drive settings - and adding some NodeJS code for the trigger; whatever it shall do. one can eg. send emails and/or FCM push notifications alike that. that trigger could also be triggered from App Script, with UrlFetchApp and there is the App Script API. one can have several triggers, which are performing different tasks (App Script is only one possibilty).

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
1

Cicada,

We have done similar functions to receive webhooks/API calls many times. Notes:

  1. to get R, you need: var Response = R.parameters and then you can do Response.kind, Response.id, etc.

  2. Logger will not work with doGet() and doPost(). I set it up a write to spreadsheet -- before any serious code. That way I know if it is getting triggered.

Weehooey
  • 988
  • 1
  • 9
  • 20
  • Thanks Weehooey! I will take a trial on your comment this week. – Pat Aug 02 '14 at 02:38
  • Hi Weehooey, I have revised my code in the post. Please take a look. 1. Revise doPost() 2. Show other the rest of my apps for your checking. But they are just some standard doGet() and authorization codes. As you see, I add this into the first statement inside doPost() to skip any possible error. It prove that it has never be pushed! MailApp.sendEmail(SysEmail, 'Testing ', 'Successfully to received Push Notification'); – Pat Aug 02 '14 at 04:35
  • Remarks: In Google Developers Console/APIS & AUTH/Push, I have add 3 domains: 1.script.google.com a) the document show that domain with https is needed. Since I have no https, that is another reason to use https://script.google.com/a/macros/power-origin.com/s/.../exec as webhook instead. b) But it doesn't let me to add whole domain, but script.google.com. Not sure if it affect the result. 2.my-domain a) It is not https, but Google accept my input. It is by Google Sites. – Pat Aug 02 '14 at 04:53
  • Cicada, you might need to add another domain: script.googleusercontent.com -- this is where the reply comes from. Also, if you hit https://script.google.com/a/macros/power-origin.com/s/.../exec with a browser and append: ?test=testing and look for R.parameters.test , it should give you the value 'testing'. Also remember to create and publish each new Project version (or use /dev instead of /exec). – Weehooey Aug 04 '14 at 19:26
  • Hi Weehooey, 1. I have updated publish each time after modification. 2. "script.googleusercontent.com" is not allowed to add into the domain option. 3. By using ?test=testing etc., I can test doGet(), it has no problem. But as I know, notification is "posted" by Drive.Changes.watch, so I think I need to use doPost() to receive the notification. But no matter doGet() or doPost(), they have not receive any pushed in changes notification after I make changes on Drive. – Pat Aug 05 '14 at 04:45
  • Cicada, if your doGet() is working, it is reasonable to assume the doPost() will work with the same code. A more complicated test would be to use the Google Apps Script UrlFetch to hit it with a POST. Unfortunately, my experience is limited to the Apps Script and I have not used the Drive API webhooks (have used webhooks with other services) -- so I cannot explain why Drive it is not hitting your URL. – Weehooey Aug 05 '14 at 13:17
  • Hi Weehooey, I know Urlfetch. I think the current problem is not on doPost(). As you see, I put the logging by email statement in the very start of doPost(), so whenever doPost is triggered, I can receive a message, no matter if its after code has problem or not. Since it has never been triggered, the problem should be if "Drive.Changes.watch(Channel)" can work or not. Since Drive.Changes.watch(Channel) is undocumented in Google Script version but just Google API, it is not clear on how to use it. Its reliability is also a question. That's why programming on Google Script is lonely. – Pat Aug 06 '14 at 01:40
  • Except this official website, could you recommend any other FAQ forum? Besides, do you know the official website introducing "script.googleusercontent.com" ? You are already very helpful. Thanks a lot. – Pat Aug 06 '14 at 01:41
  • You are welcome. script.googleusercontent.com is mentioned on the Google Apps Script site > Storing and Serving Data > Content Service (at the end of the page under redirects). I would re-ask your question here in a different way -- Stack Overflow is great. – Weehooey Aug 06 '14 at 02:15
  • I was also considering what is the best question approach in this forum. Agree. Thanks again for your help. – Pat Aug 06 '14 at 08:17