5

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.

darul75
  • 343
  • 6
  • 21
  • I have also tried directly with Drive API call instead of native as described [here](https://developers.google.com/apps-script/advanced/drive) but at the end get the logical message "The current scope does not allow use of the appdata folder" – darul75 Sep 19 '16 at 19:10
  • I just added a feature request for a helper function that would do this, as well as allow the script editor to manage the permission more rationally, please star/up vote if you concur: https://code.google.com/p/google-apps-script-issues/issues/detail?id=6592 – Timothy Johns Jan 04 '17 at 22:31
  • Have you managed to solve this? I am looking for the same... – ZorgoZ Jun 23 '17 at 12:09
  • Does your script have an `appsscript.json` file? You can add the necessary scope (assuming you know what it is, they are largely undocumented I've found) to the `"oauthScopes": ["...","..."]` array in that object. – wulftone Oct 02 '17 at 22:24

2 Answers2

1

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.

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
-1

You've enabled Drive v2 API and use the alias appDataFolder which is for Drive v3. That may have contributed to the issue you're encountering.

If you'll be using v2, try to change the alias to appfolder. Hopefully this has been of help to you.

Happy coding!

adjuremods
  • 2,938
  • 2
  • 12
  • 17
  • Thanks for replying but still same issue "Access denied: DriveApp." – darul75 Sep 20 '16 at 10:24
  • The Drive API version is not the issue. It has to do with the particular scope associated with the appDataFolder (https://www.googleapis.com/auth/drive.appdata). This scope has to be explicitly granted by the user and currently that can only be done by setting up an OAuth flow. – TheAddonDepot Aug 02 '17 at 18:01