Search code examples
google-apps-scriptgoogle-cloud-platformpush-notificationtriggersgoogle-drive-api

Detect when a File has Been Uploaded to Google Drive in GAS


I'm looking for a way to trigger a GAS (google app script) when I upload a file to google drive. I was pointed to the push notification API. I can't get it to work with GAS. There are a lot of questions about this on StackOverflow, but they were all 5+ years ago. Any change since then? Can I do this without using cloud functions?

Questions I've looked through:

And much more I've not mentioned here.

I am also aware you can do this with a timer, but waiting 15 minutes for the change to take effect would hurt my use by quite a bit. One mentions a [solution][1] I like, but I don't know how to make it work :

UPDATE: Gmail leverages Cloud Pubsub for push notifications and this service has recently been updated to remove the need for domain verification for push endpoints. So, going forward its now possible to use GAS Web App URLs in this scenario.


Solution

  • UPDATE (June 2022) : Domain verification is no longer a requirement for Google Drive Push Notifications.


    The Gmail API uses Cloud Pubsub to send push notifications.

    Unfortunately, the Drive API does not currently have that capability. The Drive API leverages HTTP endpoints (aka. webhooks) to receive push notifications and that requires domain verification (see documentation).

    In the past, GAS Web Apps were automatically verified when registered with the Chrome Web Store, but that option was removed from the IDE back in 2019.

    Moreover, the Drive API sends most of its notification payload as HTTP headers which cannot be accessed from a GAS Web App (doPost(e) only allows access to the POST body and URL params).

    So what you want is not currently possible using GAS, at least not by itself. If you want to remain within Google's ecosystem, you can try using a GCP Cloud Function instead (or maybe as an intermediary service), but its not free.

    Alternatively, you can setup an endpoint outside of Google's platform (for example; a URL to a PHP script hosted on your own domain), but you'll need to verify that endpoint's URL using one of the methods outlined in the documentation.