I have an issue updating the Acl for a Google Document.
I want to share the document with some users but only allow COMMENTER privileges.
This is the code:
AclEntry newAclEntry = getDservice().insert(new URL(aclFeedUrl), new AclScope( AclScope.Type.USER, “user1@domain.com”), AclRole.COMMENTER);
Now Eclipse tells me AclRole.COMMENTER is a valid type but when I run the code I get the following exception:
Service Exception: The posted entry has an invalid value for the field(s): role
If I change this to AclRole.READER it works fine.
So I thought I’d do a bit of investigation. You can set COMMENTER access from a Google Doc via ‘share’. So I did that and then read the ACL’s for the doc via:
AclFeed feed = getDservice().getFeed(new URL(doc.getAclFeedLink().getHref()),AclFeed.class);
if (feed == null)
System.out.println("Feed is null");
else {
for (AclEntry a: feed.getEntries()) {
System.out.println("acl role:" + a.getRole().getValue());
System.out.println("acl scope:" + a.getScope().getValue());
}
}
The output for user1 with COMMENTER access, and user2 with READER access was:
acl role:reader
acl scope:user1@domain.com
acl role:reader
acl scope:user2@domain.com
So even when user1 is explicitly a COMMENTER, it’s being reported as a ‘reader’
If I access the online documentation for AclRole there is no COMMENTER enumeration (https://developers.google.com/gdata/javadoc/com/google/gdata/data/acl/AclRole).
So, how do I set an ACL for Comment only privileges? Clearly it’s supported in Google Docs, and Eclipse is picking it up from the GData jars I’m using.
What am I missing?
The thing missing here is checking the
additionalRolefield of the ACL entry. The user is a reader and a commenter. Check out this raw XML response from the Documents List API:Note the value of
gAcl:additionalRoleiscommenter.Try this code to create a commenter entry:
And to determine if an entry has the
commenteradditionalRole, do this:However, you really should upgrade to the Drive API v2, which makes this easier. The following code sample is for the Drive API:
Thanks to Alain Vongsouvanh for providing parts of this answer!