Как получить всех пользователей в моем домене
-
18-09-2019 - |
Вопрос
Я создал один домен, в этом домене я создал почти 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();
}
}
}
какая проблема в этом кодировании?
Решение
Список пользователей возвращается в ленте atom.Это постраничный канал, содержащий максимум 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());