英文:
Okta get all users list without limit in java
问题
我知道通过使用这个链接头,我们可以获取到下一个用户,通过将最大限制设置为200,可以获取用户列表,如此处所定义。
是否有办法使用Java语言获取Okta中的所有用户,没有任何限制?
在这里我找到了一种在Python中获取所有用户的方法。是否有Java的解决方案?
英文:
I know by using this Link header we can get the next users and we can get the users list by passing a maximum of 200 as a limit as defined here.
Is there any way to get all the users present in Okta without any limitations by using java language?
Here I found a way to get all users in Python. Any solution for Java?
答案1
得分: 1
你可以使用 Okta 的 Java 管理 SDK 来列出所有用户,就像这样:
UserList users = client.listUsers();
// 使用流
client.listUsers().stream()
.forEach(user -> {
// 做一些操作
});
参考资料:
https://github.com/okta/okta-sdk-java#list-all-users
https://github.com/okta/okta-sdk-java#paging
英文:
You can use Okta's Java Management SDK to list all users like this:
UserList users = client.listUsers();
// stream
client.listUsers().stream()
.forEach(user -> {
// do something
});
References:
https://github.com/okta/okta-sdk-java#list-all-users
https://github.com/okta/okta-sdk-java#paging
答案2
得分: 0
我尝试将Python逻辑转换为Java。以下是获取所有用户的可工作代码:
public List<RemoteUsersDTO> getAllRemoteUsers() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String OKTA_URL = "https:bmg.okta-emea.com/api/v1/users?limit=200";
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "SSWS " + OKTA_API_KEY);
HttpEntity<String> request = new HttpEntity<>(headers);
String url = OKTA_URL;
List<RemoteUsersDTO> remoteUserList = new ArrayList<>();
while (url != null) {
ResponseEntity<RemoteUsersDTO[]> response = restTemplate.exchange(
url, HttpMethod.GET, request, RemoteUsersDTO[].class);
RemoteUsersDTO[] remoteUsers = response.getBody();
HttpHeaders responseHeaders = response.getHeaders();
List<String> links = responseHeaders.get("Link");
if (links != null) {
for (String link : links) {
if (link.contains("next")) {
String urlNext = StringUtils.substringBefore(link, ";");
urlNext = urlNext.substring(1, urlNext.length() - 1);
url = urlNext;
} else {
url = null;
}
}
} else {
url = null;
}
if (remoteUsers != null && remoteUsers.length > 0) {
remoteUserList.addAll(Arrays.asList(remoteUsers));
log.info("Total remote users from okta: " + remoteUserList.size());
} else {
url = null;
}
}
return remoteUserList;
}
需要进行优化,但目前已经可以正常工作。
英文:
I tried to convert the python logic into Java. Here is the working code to get all users:
public List<RemoteUsersDTO> getAllRemoteUsers() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String OKTA_URL = "https:bmg.okta-emea.com/api/v1/users?limit=200";
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "SSWS " + OKTA_API_KEY);
HttpEntity<String> request = new HttpEntity<>(headers);
String url = OKTA_URL;
List<RemoteUsersDTO> remoteUserList = new ArrayList<>();
while (url != null) {
ResponseEntity<RemoteUsersDTO[]> response = restTemplate.exchange(
url, HttpMethod.GET, request, RemoteUsersDTO[].class);
RemoteUsersDTO[] remoteUsers = response.getBody();
HttpHeaders responseHeaders = response.getHeaders();
List<String> links = responseHeaders.get("Link");
if (links != null) {
for (String link : links) {
if (link.contains("next")) {
String urlNext = StringUtils.substringBefore(link, ";");
urlNext = urlNext.substring(1, urlNext.length() - 1);
url = urlNext;
} else {
url = null;
}
}
} else {
url = null;
}
if (remoteUsers != null && remoteUsers.length > 0) {
remoteUserList.addAll(Arrays.asList(remoteUsers));
log.info("Total remote users from okta: " + remoteUserList.size());
} else {
url = null;
}
}
return remoteUserList;
}
Need to optimize it but it is working as of now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论