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

13,923

Solution 1

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.

Solution 2

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

Share:
13,923
user1766394
Author by

user1766394

Updated on June 25, 2022

Comments

  • user1766394
    user1766394 almost 2 years

    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?