husani0nl9
husani0nl9

Reputation: 53

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.

Upvotes: 1

Views: 2339

Answers (3)

danielx
danielx

Reputation: 1793

The Drive API Permission resource has four types.

  • user
  • group
  • domain
  • anyone

The emailAddress field is only present when the type is user or group and the domain field is only present on type domain.

Also make sure you are explicitly asking for the emailAddress field by using the fields query parameter.

Upvotes: 1

nightlytrails
nightlytrails

Reputation: 2692

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.

Upvotes: 1

Peter
Peter

Reputation: 5601

If the user account you're using to run the program is in a different GSuite (Google Apps) domain than the owner of the file then you won't have the authorization to access their email address. Your options are:

  1. Use the displayName instead of the email address.
  2. Run your program with a Google account in the same GSuite domain
  3. Run your program with delegated authority to be able to impersonate users of a GSuite domain (see the section titled "Delegating domain-wide authority to the service account" here

Upvotes: 1

Related Questions