我終於找到了如何做到這一點。基本代碼,包括身份驗證:
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());
}
});
}
}
數據類:
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;
}
這花費了大量的試驗&錯誤的嘗試。