2011-11-22 22 views
0

我想枚舉可在我的Google Apps For Business域中使用的組(及其成員),但我在查找相關API時遇到問題。 documentation page上的所有可用內容似乎都假設我已經知道該組名 - 即使這樣,也無法列出這些成員。如何枚舉我的域中的Google網上論壇並列出其成員?

的API,GDATA的前一個版本,有說我打算做一切clear interface - 但它並沒有提供一個Maven包(事實上,它明確指出no such package will be provided)和開發團隊是suggesting that the new interface is preferable

那麼是否有可能使用Google API以與使用GDATA API相同的方式枚舉組?

回答

0

我終於找到了如何做到這一點。基本代碼,包括身份驗證:

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; 
} 

這花費了大量的試驗&錯誤的嘗試。

相關問題