0

Purpose: If you read the first paragraph of the following link: https://developers.google.com/drive/api/v3/appdata Now as you can see there is no option to do this using AppScript. I want to try creating an Application Data Folder using Google Sheets editor.

Is this possible? If not, am I missing something? Is my logic wrong?

Adam
  • 63
  • 7
  • It is possible to access the drive api from apps script using advanced Google services or urlfetch. See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for more details. – TheMaster May 19 '20 at 07:19

1 Answers1

1

1. You can use any Drive API method in Apps Script by enabling the Advanced Drive Service

The syntax would be Drive.Files.insert(resource, mediaData, optionalArgs)

2. You can perform an Url fetch request

Whereby the syntax is UrlFetchApp.fetch(url, options);

and the url and options can be deduced from the method description.

UPDATE

A sample how to create a file in the application folder with url fetch: Code.gs

function uploadToSharedDrive(){
  var url = "https://www.googleapis.com/upload/drive/v3/files?supportsTeamDrives=true&uploadType=resumable"; 
  var blob = Utilities.newBlob('This is the content!'); 
  var metaData = {
    'name' :'config.json',
    'parents' :["appDataFolder"]
  }
  params = {
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()   
    },
    contentType:  'application/json',    
    method: 'post',
    payload: JSON.stringify(metaData),
  }
  var response = UrlFetchApp.fetch(url, params);
  var data = blob.getBytes();
  var params2 = {
    method: "put",
    payload: data,
    muteHttpExceptions: true,
  };
  location = response.getHeaders().Location;
  var response = UrlFetchApp.fetch(location, params2);
  Logger.log(response.getContentText())   
}

For this code to work correctly, you need to edit the manifest and provide the necessary scopes.

manifest.js:

{
...
   "oauthScopes": ["https://www.googleapis.com/auth/drive.appdata", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request"]
...
}

Keep in mind that the file you create will not be accessible outside of the App - this is intended behavior.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • I really don't understand what was explained on top, I am new to appscript and I am confused by the above explanation. @ziganotschka – Adam May 21 '20 at 10:12
  • I assume that you know how to use the Drive API? So what I tried to explain is that you can make calls to the Drive API from inside Apps Script. It is just called `Drive` instead of `Drive API`, but it offers you the same methods like the Drive API`. Please refer to the linked documentation to see how to enable and use `Drive`. If you still have questions after this, please specify what exactly is not clear. – ziganotschka May 21 '20 at 10:26
  • Enabling the Drive API is not the problem, I am finding it difficult to create a application data folder which is hidden to users.. Basically when a User installs an app, a folder inside that app is created which is not accessible or visible to the users. I want to create that folder which is only accessible by a developer. – Adam May 23 '20 at 05:52
  • Are you able to perform this request successfully with the Drive API without Apps Script? – ziganotschka May 23 '20 at 17:00
  • No I have not performed this request without appscript. – Adam May 26 '20 at 04:23
  • As you can draw from the samples in the documentation you linked, you need to perform a resumable or multipart upload in order to specify both metadata and the content of the file and you need to specify `appDataFolder` as a parent. I will update my answer with a sample how to do in Apps Script, but to gain a deeper understanding for the code,you would need to understand first how this request is working with the API. – ziganotschka May 26 '20 at 10:15