3

How can I use the Drive's Push Notifications API in conjunction with the GAS Published app?

I've setup the folowing App below that appends anything that receives via GET/POST into this sheet for testing, and a failed attempt of adding the WATCH into the published URL, I get the error "Insufficient Permission".

Can I use the App even if I can't use the authorization options present in the documentation?

Link to the script file below

var sslog = SpreadsheetApp.openById('1RIxhdCQlZ52-GJG43m4fdMiCLSCtXjK62YxY4M2aXkc').getSheets()[0];

function doPost(e) {
 sslog.appendRow([e]);
  return 200;
}

function doGet(e) {
 sslog.appendRow([e]);
  return 200;
}

function addWatch(){
  var PayLoad = {
    "id": "4baa4sd80-6ass-1asd4e2-bs5fd-0asd848c9a77", // Your channel ID.
    "type": "web_hook",
    "address": "https://script.google.com/macros/s/AKfycbwBoiIBIJaAJVWJb5Tboc_Wz0RNDxaD_8raKnnLWO_WllO3Lnfe/exec", // Your receiving URL.
    "expiration": ((new Date()).getTime() + 1000*60*5)// (Optional) Your requested channel expiration time.
  }
  var headers = { 'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(), 'Content-Type': 'application/json' };
  var url = 'https://www.googleapis.com/drive/v2/files/15flCrMJ4ItmPZzVzdfmOFuz44iG7xSIqSV066BW7G-Q/watch';

  var mod = UrlFetchApp.fetch(url, { headers:headers, payload:JSON.stringify(PayLoad), method:'POST' });
}

I've tried to use the Drive.Changes.watch(resource, optionalArgs) from here, but dunno what to use as resource/args.

Kriggs
  • 3,731
  • 1
  • 15
  • 23

1 Answers1

3

Caveat: I haven't been able to get this working myself, which was only frustrating until I finally looked for a reported issue. Sure enough, this isn't supported, and is considered a feature request. ("Oh, you wanted an API that works! That wasn't explicit in the specification, so now it's a feature request. We'll get around to it Real Soon Now.")

The uselessness of Drive.changes.watch() is spelled out in Issue 4254: Allow Apps Script webapps to receive and respond to (Drive) push notifications. In the comments for that issue, it's mentioned that this feature gap was described in a presentation shared on YouTube, and "discussed" on StackOverflow in Use Google Script's Web App as Webhook to receive Push Notification directly. (Not much actual info in that Q&A, since it turns out the answerer had never actually tried the Drive notification webhook.) The key comment from the issue report is #6, where Googler "ryanr..." states that Google Apps Script is not supported as a (direct) notification receiver.

However...

If you have an external server that you control, you can use it to receive notifications, and set that up from Google Apps Script. First, use the Developer's Console from your script to enable the Drive API and to set the project up for push notifications. This will also take you through the steps to confirm control of the target domain.

dev console

v

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • So, the answer is: I can't. I looked up Pat's question but thought that a year later something should've changed, but failed to find issue #4254, starred and following it. Thanks alot for the time in the research, I know it's time consuming, but I had run out of ideas. I'm in talk with the company's servers admin to release me a redirect channel to accomplish this, since it's the only way. Thanks again! – Kriggs Jul 07 '15 at 11:26
  • NP! Wish it had been more fruitful. – Mogsdad Jul 07 '15 at 11:33
  • 1
    Thanks! So disappointing. Most answers on this topic date back several years. Have there been any new developments? Answers like these https://stackoverflow.com/questions/30584533/unable-to-create-subscription-with-a-push-end-point-on-app-script-script-google suggest this can be set up but no matter what I do, Google won't let me to verify the GAS web app URL even if it's published as an add-on in draft mode. – Anton Dementiev Jul 08 '18 at 16:15