Kriggs
Kriggs

Reputation: 3778

Use a GAS published web app as a handler for Drive push notifications

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.

Upvotes: 3

Views: 463

Answers (1)

Mogsdad
Mogsdad

Reputation: 45740

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

Upvotes: 3

Related Questions