Как получить всех пользователей в моем домене - PullRequest
3 голосов
/ 05 февраля 2010

Я создал один домен в этом домене, я создал около 500 учетных записей пользователей. Я хочу получить всех пользователей в моем домене. Поэтому я использую следующую кодировку для получения всех пользователей в моем домене. Но в этой кодировке я отобразил только первые 100 пользователей. И он также отображает общее количество записей пользователей 100. Я не знаю, что проблема в этой кодировке.

import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * This is a test template
 */

  public class AppsProvisioning {

    public static void main(String[] args) {

      try {

        // Create a new Apps Provisioning service
        UserService myService = new UserService("My Application");
        myService.setUserCredentials(admin,password);

        // Get a list of all entries
        URL metafeedUrl = new URL("https://www.google.com/a/feeds/"+domain+"/user/2.0/");
        System.out.println("Getting user entries...\n");
        UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);
        List<UserEntry> entries = resultFeed.getEntries();
        for(int i=0; i<entries.size(); i++) {
          UserEntry entry = entries.get(i);
          System.out.println("\t" + entry.getTitle().getPlainText());
        }
        System.out.println("\nTotal Entries: "+entries.size());
      }
      catch(AuthenticationException e) {
        e.printStackTrace();
      }
      catch(MalformedURLException e) {
        e.printStackTrace();
      }
      catch(ServiceException e) {
        e.printStackTrace();
      }
      catch(IOException e) {
        e.printStackTrace();
      }
    }
  }

какая проблема в этой кодировке?

1 Ответ

2 голосов
/ 06 февраля 2010

Список пользователей возвращается в ленте атомов. Это страничный фид, максимум 100 записей на странице. Если в ленте больше записей, тогда будет элемент atom: link с атрибутом rel = "next", указывающим на следующую страницу. Вам нужно продолжать переходить по этим ссылкам, пока не останется больше «следующих» страниц.

См .: http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html#Results_Pagination

Код будет выглядеть примерно так:

URL metafeedUrl = new URL("https://www.google.com/a/feeds/"+domain+"/user/2.0/");
System.out.println("Getting user entries...\n");
List<UserEntry> entries = new ArrayList<UserEntry>();

while (metafeedUrl != null) {
    // Fetch page
    System.out.println("Fetching page...\n");
    UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);
    entries.addAll(resultFeed.getEntries());

    // Check for next page
    Link nextLink = resultFeed.getNextLink();
    if (nextLink == null) {
        metafeedUrl = null;
    } else {
        metafeedUrl = nextLink.getHref();
    }
}

// Handle results
for(int i=0; i<entries.size(); i++) {
    UserEntry entry = entries.get(i);
    System.out.println("\t" + entry.getTitle().getPlainText());
}
System.out.println("\nTotal Entries: "+entries.size());
...