0

I need to change the permission of every uploaded file. But when I try to add this code,

printPermissionIdForEmail(email) {
var request = gapi.client.drive.permissions.getIdForEmail({
  'email': email,
});
request.execute(function(resp) {
  return ('ID: ' + resp.id);
});

}

I got an error of getIdForEmail is not a function.

gapi.client.init, gapi.auth2.getAuthInstance(), 

are working. But why gapi.client.drive.permissions.getIdForEmail is not working? There is something I need to do? in Google Developers Page? in my Code?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
JMA
  • 974
  • 3
  • 13
  • 41

2 Answers2

3

getIdForEmail is a method only available in Google Drive v2.

With V3 you are going to have to go after it in another manner.

Do a files.list with the q parameter. In the q parameter supply the user whos permissions you wish to change. You can see here how to use search This would find all the files where someuser is the owner.

'someuser@gmail.com' in owners

Then you will get a list of file resources you can then check the permissions on each file using permissions.list and use that to change the ones you need.

I am not a JavaScript developer but I found this in the documentation it shows how to use search to list files.

  /**
   * Print files.
   */
  function listFiles() {
    gapi.client.drive.files.list({
      'q': "'someuser@gmail.com' in owners",
      'fields': "*"
    }).then(function(response) {
      appendPre('Files:');
      var files = response.result.files;
      if (files && files.length > 0) {
        for (var i = 0; i < files.length; i++) {
          var file = files[i];
          appendPre(file.name + ' (' + file.id + ')');
        }
      } else {
        appendPre('No files found.');
      }
    });
  }

Update:

I just spotted this. About.get Gets information about the user, the user's Drive, and system capabilities

{
 "user": {
  "kind": "drive#user",
  "displayName": "Linda Lawton",
  "photoLink": "xxxx",
  "me": true,
  "permissionId": "060305882255734372",
  "emailAddress": "xxxx@gmail.com"
 }
}

Could that be the same permissionId you were looking for?

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Can you guys add answer here too, http://stackoverflow.com/questions/42788967/transfer-file-ownership-in-google-drive-api – JMA Mar 14 '17 at 14:59
0

The method I use is based on the OAuth2 library published on script.google.com. This is written for Google Apps Script with domain-wide delegation. The key here is building a valid url and option for UrlFetchApp.fetch(url, options), then parsing the result to find the ID number.

function getIdForEmailv3(userEmail) {
  var service = getService(userEmail);
  if (service.hasAccess()) {
    Logger.log('getIdForEmailv3(%s) has access', userEmail);

    var url = 'https://www.googleapis.com/drive/v3/about' + '?fields=user/permissionId'
    var options = {
      'method': 'get',
      'contentType': 'application/json',
      'headers': { Authorization: 'Bearer ' + service.getAccessToken() },
      'muteHttpExceptions': true
    };

    var response = UrlFetchApp.fetch(url, options);

    var resultString = JSON.stringify(response.getContentText());
    var regex = new RegExp(/\d+/g);
    var id = regex.exec(resultString)[0];

    Logger.log('getIdForEmailv3 returned %s for %s', id, userEmail);
    return id

  } else {

    Logger.log('getIdForEmailv3 getLastError: %s', service.getLastError());
    Logger.log('getIdForEmailv3 returned %s for %s', 0, userEmail);
    return 0;
  }
}

Regex idea from: Easiest way to get file ID from URL on Google Apps Script

Fields format from comment on solution: How to add 'field' property to a Google Drive API v3 call in JavaScript?

C02
  • 156
  • 13