Search code examples
javagoogle-apigdatagdata-apigoogle-api-java-client

How can I enumerate Google Groups in my domain and list their members?


I want to enumerate the groups (and their members) that are available on my Google Apps For Business domain, but I am having problems finding relevant API. Everything available on the documentation page seems to assume I already know the group name - and even then there's no way to list the members.

The previous version of the API, gdata, has a clear interface for everything that I intend to do - but it does not provide a Maven package (in fact it explicitly states that no such package will be provided) and the development team is suggesting that the new interface is preferable.

So is it possible to enumerate groups using Google API the same way it was possible with GDATA API?


Solution

  • I finally found out how to do this. Basic code, including authentication:

    public class GroupLister {
        static HttpTransport transport = new NetHttpTransport();
        static HttpParser parser = new AtomParser(new XmlNamespaceDictionary());
    
        public GroupFeed listGroups(String domainName, String apiKey,
                String username, String password) throws IOException {
            HttpRequestFactory factory = createRequestFactory(transport, username,
                    password);
            GoogleUrl url = new GoogleUrl(
                    "https://apps-apis.google.com/a/feeds/group/2.0/" + domainName);
            url.key = apiKey;
            return factory.buildGetRequest(url).execute().parseAs(GroupFeed.class);
        }
    
        public HttpRequestFactory createRequestFactory(
                final HttpTransport transport, final String username,
                final String password) {
            return transport.createRequestFactory(new HttpRequestInitializer() {
                public void initialize(HttpRequest request) throws IOException {
                    request.addParser(parser);
                    ClientLogin authenticator = new ClientLogin();
                    authenticator.transport = transport;
                    authenticator.authTokenType = "apps";
                    authenticator.username = username;
                    authenticator.password = password;
                    request.getHeaders().setAuthorization(
                            authenticator.authenticate()
                                    .getAuthorizationHeaderValue());
                }
            });
        }
    
    }
    

    Data classes:

    public class GroupFeed {
        @Key
        public Date updated;
        @Key("entry")
        public List<GroupEntry> groups;
    }
    
    public class GroupEntry {
        @Key
        public String id;
        @Key("apps:property")
        public List<PropertyEntry> properties;
    }
    
    public class PropertyEntry {
        @Key("@name")
        public String name;
        @Key("@value")
        public String value;
    }
    

    This took a lot of trial & error attempts.