我想枚举我的 Google Apps For Business 域中可用的组(及其成员),但在查找相关 API 时遇到问题。文档页面上的所有内容似乎都假设我已经知道组名 - 即使这样也无法列出成员。
API 的先前版本 gdata 对我打算做的所有事情都有一个清晰的接口- 但它没有提供 Maven 包(实际上它明确声明不会提供这样的包)并且开发团队建议新界面更可取。
那么是否可以像使用 GDATA API 一样使用 Google API 枚举组?
我终于找到了如何做到这一点。基本代码,包括认证:
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;
}
这需要大量的反复试验。