0

I want to create a Google App Script web app to allow users to upload files to a folder of my google drive.

The problem is that I've checked the drive scope list here,

in order to perform the upload action , this scope must be reviewed by the users

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

and it comes with a scary description See, edit, create and delete all of your Google Drive files which is way too strong.

Is there any way to perform the upload action with a less stronger scope?

Thank you so much.

Tudis Lei
  • 31
  • 1
  • 4

1 Answers1

1

In your situation, for example, how about the following scope?

https://www.googleapis.com/auth/drive.file

The official document says as follows. Ref

Per-file access to files created or opened by the app. File authorization is granted on a per-user basis and is revoked when the user deauthorizes the app.

In this scope, only the files and folders created by this application can be accessed.

Reference:

Added:

From your following replying,

Thank you for replying too, I have an update. When I run the code in the editor , it throws an error : ' { [Exception: You do not have permission to call DriveApp.Folder.createFile. Required permissions: googleapis.com/auth/drive] name: 'Exception' }' So I guess there is no solution to my problem T.T

I understood that you are using createFile with the Drive service (DriveApp). In this case, the scope of https://www.googleapis.com/auth/drive is requierd to be used. It seems that this is the current specification. In order to use the scope of https://www.googleapis.com/auth/drive.file, how about using Drive API at Advanced Google services? By this, you can use the scope by setting to appsscript.json which is the manifest file.

At first, please add https://www.googleapis.com/auth/drive.file as "oauthScopes": ["https://www.googleapis.com/auth/drive.file"] to the manifest file of appsscript.json. Ref

Although I'm not sure about your actual script, the sample script for creating a Spreadsheet is as follows.

Sample script:

Before you use this script, please enable Drive API at Advanced Google services.

function myFunction() {
  const obj = Drive.Files.insert({title: "sampleSpreadsheet", mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: "###folderId###"}]});
  console.log(obj.id)
}
  • When you run this script, new Spreadsheet is created to folderId by the scope of https://www.googleapis.com/auth/drive.file.

Note:

  • If you are required to use other scopes, please add them to oauthScopes.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Nope , this scope won't work. Though I am not sure how to debug into it. And I've tried the case that adding all the scopes except for that 'auth/drive' one , it still didn't work. – Tudis Lei May 21 '21 at 07:49
  • @Tudis Lei Thank you for replying. I apologize for the inconvenience. Unfortunately, from `Nope , this scope won't work. Though I am not sure how to debug into it , but I've tried the case that adding all the scopes except for that 'auth/drive' one , and It still didn't work.`, I cannot image your situation. I apologize for my poor English skill. So, can I ask you about the detail of it? By this, I would like to confirm it. – Tanaike May 21 '21 at 07:50
  • Thank you for replying too, I have an update. When I run the code in the editor , it throws an error : ' { [Exception: You do not have permission to call DriveApp.Folder.createFile. Required permissions: https://www.googleapis.com/auth/drive] name: 'Exception' }' So I guess there is no solution to my problem T.T – Tudis Lei May 21 '21 at 07:58
  • @Tudis Lei Thank you for replying. From the script in your replying, I understood that you are using `DriveApp.Folder.createFile` of the Drive service. In this case, the scope is automatically `https://www.googleapis.com/auth/drive`. It seems that this is the current specification. But, when Drive API at Advanced Google services is used, you can select the scope of `https://www.googleapis.com/auth/drive.file` by `appsscript.json`. So how about using Drive API instead of the Drive service (`DriveApp`)? – Tanaike May 21 '21 at 08:02
  • @Tudis Lei From your replying, I added one more explanation. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike May 21 '21 at 08:17
  • Thank you so much for the information about the 'Advanced Google services' part. I still have a lot more to read and to try for now , and will come back to update my progress later ! – Tudis Lei May 21 '21 at 09:24
  • hello again, do you have any sample code of uploading a local image with the Advanced Google services ? Thank you so much. – Tudis Lei May 21 '21 at 10:21
  • @Tudis Lei Thank you for replying. I'm glad your issue was resolved. About your new question, is [this thread](https://stackoverflow.com/q/60742695) useful? In your situation, Drive API is used at GAS side. Even when that was not useful, I would like to support you. At that time, can you post it as new question by including more information? By posting it as new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike May 21 '21 at 12:03