Okta get all users list without limit in java

huangapple go评论86阅读模式
英文:

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&lt;RemoteUsersDTO&gt; getAllRemoteUsers() {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
String OKTA_URL = &quot;https:bmg.okta-emea.com/api/v1/users?limit=200&quot;;
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add(&quot;Authorization&quot;, &quot;SSWS &quot; + OKTA_API_KEY);
HttpEntity&lt;String&gt; request = new HttpEntity&lt;&gt;(headers);
String url = OKTA_URL;
List&lt;RemoteUsersDTO&gt; remoteUserList = new ArrayList&lt;&gt;();
while (url != null) {
ResponseEntity&lt;RemoteUsersDTO[]&gt; response = restTemplate.exchange(
url, HttpMethod.GET, request, RemoteUsersDTO[].class);
RemoteUsersDTO[] remoteUsers = response.getBody();
HttpHeaders responseHeaders = response.getHeaders();
List&lt;String&gt; links = responseHeaders.get(&quot;Link&quot;);
if (links != null) {
for (String link : links) {
if (link.contains(&quot;next&quot;)) {
String urlNext = StringUtils.substringBefore(link, &quot;;&quot;);
urlNext = urlNext.substring(1, urlNext.length() - 1);
url = urlNext;
} else {
url = null;
}
}
} else {
url = null;
}
if (remoteUsers != null &amp;&amp; remoteUsers.length &gt; 0) {
remoteUserList.addAll(Arrays.asList(remoteUsers));
log.info(&quot;Total remote users from okta: &quot; + remoteUserList.size());
} else {
url = null;
}
}
return remoteUserList;
}

Need to optimize it but it is working as of now.

huangapple
  • 本文由 发表于 2020年9月17日 02:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63925983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定