0

I was able to create google sheet using service account, however unable to insert permissions to the sheet for users :

function createSheet(jwt, apiKey) {
  const sheets = google.sheets({version: 'v4'});
  var request = {
    resource: {
      "properties": {
        "title": "testSheet"
      }
    },
    auth: jwt 
  };

Below is the response from create sheet api:

config: {transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", xsrfHeaderName: "X-XSRF-TOKEN", …}
data: {spreadsheetId: "XXX", properties: {…}, sheets: Array(1), spreadsheetUrl: "https://docs.google.com/spreadsheets/d/XXX/edit"}
...
status: 200
statusText: "OK"

My code.

function insertPermission(fileId) {
  var body = {
    'value': 'xxx@gmail.com',
    'type': 'user',
    'role': 'owner'
  };
  var drive=google.drive({version: 'v3'});
   drive.permissions.create({
    'fileId': fileId,  //sheetID returned from create sheet response
    'resource': body
  }, function(err, response) {if (err) {
    console.error(err);
    return;
    } else{
      console.log(JSON.parse(JSON.stringify(response))) ;
    }
  });

Error from insertPermission :

index.js:114 Error: Insufficient Permission at createError

alternately, Is there a way to specify file permissions in sheets.create api ?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Karuna
  • 3
  • 2
  • Resolved the issue here.Point to note about a service account is that it is not the same as your personal account.Copy service account email address and give it access to a directory on your Google drive. In addition user drive api to create file instead of sheets api - – Karuna Dec 03 '18 at 09:17
  • In addition, there is a setup on gdrive to create folder and give service account permissions on the folder for read/write. MyDrive is accessible by user's id and not by service account. Issue is resolved now. – Karuna Dec 10 '18 at 07:52

1 Answers1

1

Insufficient Permission at createError

Normally means that the user you are logged in as does not have permission to do what they are trying to do.

  1. Are you sure that its the service account that is running this insert as it owns the file only it can transfer ownership.
  2. How did you login as the service account do you have anything like scopes do you have one of the write scopes added.

if you check transferring_ownership

var fileId = '1sTWaJ_j7PkjzaBWtNc3IzovK5hQf21FbOw9yLeeLPNQ';
var permissions = [
  {
    'type': 'user',
    'role': 'writer',
    'emailAddress': 'user@example.com'
  }, {
    'type': 'domain',
    'role': 'writer',
    'domain': 'example.com'
  }
];
// Using the NPM module 'async'
async.eachSeries(permissions, function (permission, permissionCallback) {
  drive.permissions.create({
    resource: permission,
    fileId: fileId,
    fields: 'id',
  }, function (err, res) {
    if (err) {
      // Handle error...
      console.error(err);
      permissionCallback(err);
    } else {
      console.log('Permission ID: ', res.id)
      permissionCallback();
    }
  });
}, function (err) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    // All permissions inserted
  }
});
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449