Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DriveApp.getFolderById results in "you do not have permission" error

I am having issues with the getFolderById function. I am the owner of the folder in question. The specific call is as follows.

var folder = DriveApp.getFolderById('string_id_of_my_folder');

I am passing the long string of the ID of the folder inside the function. I ran the function inside the editor to make sure permissions have been turned on - and I'm not getting any errors.

But when I try to run the function inside the spreadsheet, I get the following error message: "You do not have permission to call getFolderById".

What am I doing wrong?

like image 746
user1766394 Avatar asked Oct 19 '22 17:10

user1766394


2 Answers

The issue here is that custom functions & simple triggers run with limited permissions: you can't perform actions that require user credentials, like reading from another file. These limitations are spelled out here for custom functions & here for simple triggers.

Installable triggers don't have this limitation. So if you needed to access another when you open your current file or from a standalone script, you'll need to install an Open trigger. Here's Google's documentation.

like image 112
Dean Ransevycz Avatar answered Oct 21 '22 23:10

Dean Ransevycz


Without setting the OAuth scopes you get something like below... enter image description here

So the specific answer to this is you need to set OAuth scopes with this

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

in the appscript.json file. To find view this file you need to go to the settings of the App Script project. enter image description here

Whenever you're using any of the Google API's you need to add the scopes from here. This will then ask you again for permission by logging into your account.

{
 "timeZone": "Europe/London",
 "dependencies": {
  "enabledAdvancedServices": [
   {
    "userSymbol": "Classroom",
    "version": "v1",
    "serviceId": "classroom"
   }
  ]
 },
 "oauthScopes": [
  "https://www.googleapis.com/auth/drive",
 ],
 "exceptionLogging": "STACKDRIVER",
 "runtimeVersion": "V8",
 "webapp": {
  "executeAs": "USER_DEPLOYING",
  "access": "MYSELF"
 }
}

You should now see something like this enter image description here

like image 24
Azmol Avatar answered Oct 21 '22 22:10

Azmol