Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing file to Application Data Folder on Add-ons from Google App Script server code

As mentioned in this document about storing application data, I would like to make it possible from a Google App Script Add-on server code.

I was able to create an arbitrary file into my google root drive folder this way.

  DriveApp.createFile('Test', 'Test content');

But was not able to figure out how to create it into (hidden) app data folder:

var dir = DriveApp.getFolderById("appDataFolder");
var file = dir.createFile('Test', 'Test content');

Receiving "Access denied" by executing it.

I guess I have to apply this following scope to my app but do not know how apply it on a google app script.

Scope: https://www.googleapis.com/auth/drive.appdata

Drive API is well activated.

Advanced google services

It would be nice if I could update these scopes (File>Project Properties menu):

Project properties scopes

Any help would be so appreciated.

like image 499
darul75 Avatar asked Nov 20 '22 17:11

darul75


1 Answers

Users have to explicitly grant your Add-on access to the private AppData folder. And currently that's only achievable by setting up an OAuth2 flow.

EDIT As of November 2017, Apps Script now supports explicit OAuth scopes. There is no longer a need to set up a custom OAuth flow, simply set the desired OAuth scope in the manifest file (as documented here) to request access to a user's App Data Folder.


Here's a video that provides a good overview of how OAuth2 works (independent of Apps Script)

OAuth 2.0: An Overview


If you're up to the task, you can review Google's OAuth2 documentation and figure out how to implement it for your use case, but you're better off leveraging the "official" OAuth2 library for Apps Script.

Here's another video that covers OAuth flows for Add-ons using said library:

OAuth2 flows in Add-ons

Note: Make sure that you use the appropriate scope(s) in your flow. In this case you only need to use one; https://www.googleapis.com/auth/drive.appdata


Also, if you're looking for some code implementing an OAuth flow to access the App Data folder check out this github repo from Spencer Easton:

Apps-Script-Appfolder-Library


The 2nd video is from Totally Unscripted, a vlog hosted by app script devs from the Google+ App Script community. Check it out, its a great resource for App Script development.

like image 66
TheAddonDepot Avatar answered Dec 04 '22 06:12

TheAddonDepot