Add Okta user to Okta group Python sdk

1.3k views Asked by At

Is there a method to add a user to an Okta group? I see update_group, but nothing specific on how to add a user to an Okta group.

1

There are 1 answers

0
kevlened On

The latest updates to the SDK have add_user_to_group(group, user) and add_user_to_group_by_id(gid, uid) methods on UserGroupsClient.

from okta import UserGroupsClient
from okta.models.usergroup import UserGroup
from okta import UsersClient
from okta.models.user import User

groups_client = UserGroupsClient('your_url', 'your_key')
users_client = UsersClient('your_url', 'your_key')

# Create group
group = UserGroup(name='sample_name', 
                  description='sample description')
group = groups_client.create_group(group)

# Create user
user = User(login='[email protected]',
            email='[email protected]',
            firstName='Joe',
            lastName='Schmoe')
user = users_client.create_user(user, activate=False)

groups_client.add_user_to_group(group, user)
# or
groups_client.add_user_to_group_by_id(group.id, user.id)

To get the latest:

pip install git+git://github.com/okta/oktasdk-python@master