Is it possible to tailor DocsList \ DriveApp permissions? - google-apps-script

I need to access the user's Google drive. All I need is to create a folder and create some files in it. Nothing more. But the permission granted by the API allows me to manage and view the user's whole drive.
So how can I specify the folder I need to access and what can I do (i.e. create files, delete files, rename files..etc)
I'm currently using the DocsList API.

Not possible. Dont use docsList as its deprecated. Use driveApp.
In apps script you have only two options:
1) permission to the entire drive read/write, like you have now.
2) permission for the 'current doc' only (read/write) but it only applies to container scripts and add-ons by using a special comment. This cant be used in webapps. See https://developers.google.com/apps-script/guides/services/authorization#manual_authorization_scopes_for_sheets_docs_and_forms

Related

Share the Drive file with extra permissions

I have a drive file where I need to restrict the users from these (I mean ticks for both needs to be removed and shared using apps script/drive api)
I need to accomplish using Google Apps Script Advanced Drive service or regular apps script. I am unsure what is the exact method. I have tried with
function shareit() {
DriveApp.getFileById(id).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
}
Drive Sharing Permissions with App Script
I manage to make both changes by using Advance Services from App Script. I was able to use part of your code to first remove the "Editors can change permissions and share" by using the:
setShareableByEditors(false)
Code Sample:
function shareit() {
DriveApp.getFileById(id).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT).setShareableByEditors(false);
// Add advance service to be able to run a files patch and update the permissions of the ID to require writters permisions to copy, print and download.
Drive.Files.patch({copyRequiresWriterPermission:true}, id);
}
As presented in the comments you would need run a patch update to the File ID to change the permissions.
References:
https://developers.google.com/apps-script/advanced/drive
https://developers.google.com/drive/api/v2/reference/files/patch
https://developers.google.com/apps-script/reference/drive/file#setShareableByEditors(Boolean)

How to access a Drive file from addon

I have developed a Sheets editor addon that uses the SpreadsheetApp to create a new spreadsheet that will contain a subset of data in the current spreadsheet. The problem is that SpreadsheetApp creates the file in the users root directory, and I would like to move it to same directory as the parent spreadsheet.
This was possible and worked during development, but now that I am trying to publish it, it is failing due to the Drive oauth scope being "restricted" now (despite the fact that it is currently published "internal" only, which the docs state should bypass this restriction, even though it is not [unless I am misunderstanding or misconfiguring something]).
Is there any way to achieve this without having to use a restricted scope? The Drive API contains a drive.file scope that allows access to files created using the current app, but the built in DriveApp does not.

How do I use the drive.file auth scope in my Gmail addon?

I'm trying to be a good Google Apps Scripter and use less permissive scopes in my app, but I can't get it to work with https://www.googleapis.com/auth/drive.file.
I have a Gmail addon that needs to create files and folders in the user's Google Drive. Right now I have it working fine with the https://www.googleapis.com/auth/drive scope in the manifest so I know the code itself functions. But when I change the scope to https://www.googleapis.com/auth/drive.file, none of the DriveApp methods work anymore and I get an error saying I need the https://www.googleapis.com/auth/drive scope.
In the DriveApp docs it does say it needs the "drive" scope, but wouldn't that mean no app could ever save data to a user's Drive without having full access to all their files? That sounds crazy. Or am I SOL? I still don't quite understand how the scopes work or if I need to do any other changes elsewhere to make them work.
It's not possible as there aren't any DriveApp methods that require that scope. Ref. https://developers.google.com/apps-script/reference/drive/drive-app
From a comment by the OP
I'm using the Drive service. I'm using DriveApp.getFoldersByName(), DriveApp.getRootFolder(), DriveApp.createFolder(), and DriveApp.createFile().
Please read the documentation carefully. The required scopes by each method are included there.
Example
From https://developers.google.com/apps-script/reference/drive/drive-app#getrootfolder
getRootFolder()
Gets the folder at the root of the user's Drive.
Return
Folder — the root folder of the user's Drive
Authorization
Scripts that use this method require authorization with one or more of the following scopes:
https://www.googleapis.com/auth/drive
Success! After going through the Drive API I figured out I had to enable the Drive API and use Drive.Files.insert().

Creating a google drive folder on customers account

I am working on an application which requires the automatic creation of a folder on customer's Google Drive accounts as opposed to them creating this themselves.
I will have their permission to do so and won't have access to this folder. Ideally this folder would be encrypted but they could do this afterwards themselves.
Is it possible? And if it, how can I achieve that?
It can be done using the REST API. See the section called Creating a folder.
https://developers.google.com/drive/api/v3/folder

add prefix to all files and folders in google drive

How can I add a prefix or a suffix to all files/ folders in Google Drive using Google Script. The objective is that whenever a users needs to be deleted to add a suffix to all files and folders and transfer to another user so that the documents of the user can easily be tracked at a later stage.
Regards
The File.setName() method in the Drive Service will do what you want. I'd be happy to help further if you post a sample of your code.