Search code examples
node.jsgoogle-apigoogle-drive-apigoogle-api-nodejs-client

What Is it the correct syntax to set permission 'sendNotificationEmails': 'false' to avoid "message" : "Rate limit exceeded. User message:


function createPermissionOffic(auth){
  const drive = google.drive({version: 'v3', auth});

  var fileId = rootFolderId;
  var permissions = [
    {
      'type': 'user',
      'role': 'writer',
      'emailAddress': 'someemailaddress@gmail.com'
    }
  ];
  // Using the NPM module 'async'
  async.eachSeries(permissions, (permission, permissionCallback)=> {
    drive.permissions.create({
      resource: permission,
      fileId: fileId,
      fields: 'id',
    }, (err, res)=> {
      if (err) {
        // Handle error...
        console.error(err);
        permissionCallback(err);
      } else {
        console.log('Permission ID: '+ res)
        permissionCallback();
      }
    });
  }, (err)=> {
    if (err) {
      // Handle error
      console.error(err);
    } else {
      // All permissions inserted
      drive.permissions.insert(
        {
          'sendNotificationEmails': 'false'
        });
    }
  });

I am getting below error : google drive API documentation does not elaborate much.here is the error I am getting. TypeError: drive.permissions.insert is not a function at async.eachSeries


Solution

  • I believe your goal as follows.

    • You want to use sendNotificationEmail: false for Permissions: create method.
    • You want to achieve this using googleapis with Node.js.
    • You have already been able to create the permission using Drive API v3.

    Modification points:

    • drive.permissions.insert is for Drive API v2. And in this case, when you run the script, an error occurs. In order to use sendNotificationEmail: false, please use it to drive.permissions.create.
    • About Rate limit exceeded, when the array length of permissions is large, such error might occur. So at first, as a test, how about checking whether the request works using a simple situation. In your sample script in your question, the array length of 1 is used. I think that how about checking the request using it?
    • In your case, the permission ID can be retrieved by res.data.id.
    • In your script, rootFolderId is used at var fileId = rootFolderId;. When rootFolderId is the root folder ID, an error like This file is never writable. occurs. So please be careful this.

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    const drive = google.drive({version: "v3", auth});
    
    var fileId = "###"; // Please set the file ID or folder ID.
    
    var permissions = [
      {
        type: "user",
        role: "writer",
        emailAddress: 'someemailaddress@gmail.com',
      },
    ];
    
    // Using the NPM module 'async'
    async.eachSeries(
      permissions,
      (permission, permissionCallback) => {
        drive.permissions.create(
          {
            resource: permission,
            fileId: fileId,
            fields: "id",
            sendNotificationEmail: false,  // <--- Added
          },
          (err, res) => {
            if (err) {
              // Handle error...
              console.error(err);
              permissionCallback(err);
            } else {
              console.log("Permission ID: " + res.data.id);  // <--- Modified
              permissionCallback();
            }
          }
        );
      },
      (err) => {
        if (err) {
          // Handle error
          console.error(err);
        } else {
          console.log("Done");  // <--- Modified
        }
      }
    );
    

    Reference: