3

So when I retrieve a file via the Google Drive API, I see that there is an attribute called 'owners'. This is an array of owners, with values for kind, displayName, picture, isAuthenticatedUser, and permissionId.

From this information, is it possible to find out what the user's e-mail address/username is?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
user1538393
  • 33
  • 1
  • 7

3 Answers3

1

As of August 2015 the method this works for me:

file_ref.getOwner().getEmail()

It allowed me to identify who is the owner of the file that used to be shared with me, and ask him to take it out of the bin :) Here's entire script (on Win ctrl+Enter shows the console):

function checkTheFile() {
  /*  paste file id, FILE_ID obtained from its URL  */
  var fileID = "FILE_ID";
  getFileInfo(fileID);  
}

function getFileInfo(fileID) {
  /*  get the file's handle  */
  var file_ref = DriveApp.getFileById(fileID);

  /*  output various file info  */
  Logger.log("Document's title: " + file_ref.getName());
  Logger.log("Document's viewers: " + file_ref.getViewers());
  Logger.log("Document's owner: " + file_ref.getOwner());
  /*  email address:  */
  Logger.log("Document owner's email address: " + file_ref.getOwner().getEmail());
  Logger.log("Sharing Permission: " + file_ref.getSharingPermission());
}
ellockie
  • 3,730
  • 6
  • 42
  • 44
0

No, it's not possible to get owner's email at the moment from permission entities.

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
0

You can use Google Docs API.

Unfortunately, it's deprecated... Wish they could add e-mails to Google Drive API.

krtek
  • 1,055
  • 1
  • 13
  • 28
  • Thanks, but I migrated away from the Docs API with this project. What I ended up doing for those who are curious: I retrieve all of the files the users own. If the user doesn't own the file, I store the owners permission id and file name in a map. As I loop through all of the users in the domain, I get their permission ids one by one and store them in a mapping that's permissionId->username. At the end of the process, I go back and clean up the mapping I have that consists of ids->file and replace the id with the username from the data I collected in the other map. Efficient? no, but it works. – user1538393 Aug 23 '13 at 18:49
  • Interesting. But with Docs API you're able to get the e-mail also for users from other domains. With this approach you're limited only to your domain, right? – krtek Aug 26 '13 at 15:37
  • Correct. This is fine for the scope of my project as I am only interested in backing up my users files and not files that are owned by users outside of my domain. – user1538393 Sep 03 '13 at 14:46
  • I managed to get the email address. Please check my answer. Hopefully it's helpful. – ellockie Aug 21 '15 at 11:18