Search code examples
javapermissionsgoogle-drive-apifile-ownership

Can't get File Owner using Google Drive API


Trying to find the owner of a file by getting the permissions for the file, looping through them, and retrieving the user information (specifically the email address) of the permission with the role of "owner".

PermissionList filePermissions = service.permissions().list(fileID).execute();

for (Permission permission : filePermissions) {

    if (permission.getRole().toLowerCase().equals("owner")) {
        String fileOwner = permission.getEmailAddress();
    }

}

"permission.getEmailAddress()" kept returning null, so I decided to call "toPrettyString()" on every permission and it showed that the permission objects only consistently contained the "id", "kind", "role", and "type", but never "emailAddress".

The Google documentation for the Drive API lists "emailAddress" as one of the properties for permission objects, so I'm confused as to why I can't retrieve it.

I thought it might be related to the user credentials used to get the Drive Service, but even using the credentials of the file owner still yeilds the same results.


Solution

  • Google Drive REST APIs v3 would only return only certain default fields. If you need some field, you have to explicitly request it by setting it with .setFields() method.

    Modify your code like this -

    PermissionList filePermissions = service.permissions().list(fileID).setFields('permissions(emailAddress,id,kind,role,type)').execute();

    You can read more about this behavior here https://developers.google.com/drive/v3/web/migration

    Quoting from the above link -

    Notable changes

    • Full resources are no longer returned by default. Use the fields query parameter to request specific fields to be returned. If left unspecified only a subset of commonly used fields are returned.

    Accept the answer if it works for you so that others facing this issue might also get benefited.