Using OktaClient to List Users within a particular Group

Hi,

Is there a way to use OktaClient to list all uses but in a particular GROUP using a group id as a perimeter

List All Users
users, resp, err = await client.list_users()

HOW to List All Users in a particular GROUP?

Can I use Custom Headers to set a GROUP ID?

**# set custom headers**

** client.set_custom_headers({‘Custom-Header’: ‘custom value’})**

Which Management SDK are you using: Node, .NET, something else?

python

config = {
‘orgUrl’: ‘https://xxxxxxxxxxxxxxxxxxxx.okta.com’,
‘token’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ # dev account
}

okta_client = OktaClient(config)

print(okta_client)

async def main():
users, resp, err = await okta_client.list_users()
print(users)
print(resp)
print(err)

try:
for user in users:

My current implementation to get all users

import time
import asyncio
from okta.client import Client as OktaClient

config = {
‘orgUrl’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxx’,
‘token’: ‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’
}

okta_client = OktaClient(config)

print(okta_client)

all_users =

async def main():
users, resp, err = await okta_client.list_users()

print(users)

print(resp)
print(err)
all_users.append(users)

counter = 0

for user in users:
print(user.profile.first_name)
counter += 1
print(counter)
while resp.has_next():
users, err = await resp.next()
for user in users:
print(user.profile.first_name)
counter += 1
print(counter)
print(counter)

while True:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

I would like to use this but with a GROUP ID so it does not get all the users in the okta instance but only the users in the group.

Can you check out list_group_users(groupId)?

Will do, will this fetch all users in the group, or will there be pagination involved? Thanks

Is there any full working example for this function I can refer to?

Here’s a quick and dirty sample. When I tested it, I received all of users in the provided group. I’m just printing out the length of my output here to check the right number of users were returned, and when I ran it, I got an output of 617, which was a match for the membership count the Admin Console reported as well.

import asyncio

from okta.client import Client as OktaClient

config = {
    'orgUrl': okta_domain,
    'token': api_token
}
okta_client = OktaClient(config)

async def main():
    users_in_group, _, err = await okta_client.list_group_users(group_id)
    print(len(users_in_group))
    
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

That is working for the length. But my group have few thousand users in it and at max I can fetch is 1000. Does this means I have to use some pagination in it.

async def main():
users_in_group, resp, err = await okta_client.list_group_users(‘00gwj1srvApCVeG79696’)

print(users_in_group)

print(len(users_in_group))
print(resp)
print(err)
while resp.has_next():
###### just trying to set this loop #########

Ah, most likely, I just didn’t have that many users in my own test org. Our API docs do indicate that the page limit for this endpoint is 1000

Did the example for pagination in the readme help?

I am using this, should I use the if else logic in first block of user in users and then use same logic again in resp.next() block?

async def main():
users_in_group, resp, err = await okta_client.list_group_users(‘00g6hvc1tkuUBmzh1697’)

print(users_in_group)

print(len(users_in_group))
print(resp)
print(err)
######### if / else condition #########
while True:
if resp.has_next():
print(resp.has_next())
users, err = await resp.next()
print(‘This is next set of users’)
print(len(users_in_group))
######### if / else condition ######### (Same as above conditional)
else:
break

All right, I added some more users to my org to test the pagination. This ended up working for me to get the full list of users (again, I’m just counting them):

okta_client = OktaClient(config)

async def main():

    users = []

    users_in_group, resp, err = await okta_client.list_group_users("00ghtuczwIbPhbTvH356")


    while True:
        for user in users_in_group:
            users.append(user)
        if resp.has_next():
            users_in_group, err = await resp.next()
        else:
            break

    print(len(users))

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

With my new users, I now get an output of 1005

This worked. You are a Rockstar Andrea. Thanks

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.