google-sheetsgoogle-apps-script

drive.permissions.create error - too many parameters


I have the following code as part of a larger script:

Drive.Permissions.create({
  "fileId": "1RynzJ-j76TIi3XIHSQeHE6XPfBdp",
  "sendNotificationEmail": false,
  "resource": {
   "role": "viewer", 
   "type": "user",
   "emailAddress": "somebodiesemail@icloud.com"
   }
});

When executed it fails with the error:

Exception: Invalid number of arguments provided. Expected 2-3 only setFolderPermissions @ Code.gs:87

As far as I can tell I'm only passing three parameters: fileID, sendNotificationEmail and resource albeit with resource being an object.

I've looked at the references and all the parameters used are referred to. The script is bound to a Sheet and I've 'Drive' identified as the service - is this correct/ If not how do you enable an 'advanced drive api' - I can't decide whether that is different from drive service and if so how to enable it.

I've tried using the reference example to create the code and the 'create()' block is fundamentally the same. If the construct was incorrect I'd expect a different error, but stating that it has an invalid number of arguments suggest the syntax is somehow incorrect.

I'd be grateful for any advice

Following Boris' answer I modified my code to that shown below, which included adding supportAllDrives and useDomainAdminAccess. Regardless of what I try or whether I use the ID of a file or a folder, it always stops with an error and states 'file not found'. I can set the permission in Drive and I'm the manager of the folder as I'm a superadmin.

 Drive.Permissions.create({
   role: "reader",
   type: "user",
   emailAddress: userGoogleAccount
    },
    instrumentGroups[nCounter].FolderID,
    {
    sendNotificationEmail: false,
    supportAllDrives: true,
    useDomainAdminAccess: true                
});


Solution

  • The structure is a little different as well the specified permission role is invalid. Valid values are 'reader', 'commenter', 'writer', 'fileOrganizer', 'organizer', and 'owner'.

    It works:

    function DrivePermissionsCreate() {
      Drive.Permissions.create({
        role: "reader",
        type: "user",
        emailAddress: "somebodiesemail@icloud.com"
      },
        "1RynzJ-j76TIi3XIHSQeHE6XPfBdp",
        {
          sendNotificationEmail: false
        });
    }
    

    You need to enable Drive API.