如何使用 python sdk 在 okta 中创建具有凭据的用户

How to create a user with credentials in okta using python sdk

有什么方法可以使用 python

创建具有凭据的 okta 用户

创建一个没有恢复问题和答案的用户。新用户在激活后可以使用分配的密码立即登录。此流程在开发自定义用户注册体验时很常见。

curl -v -X POST \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: SSWS ${api_token}" \
-d '{
  "profile": {
    "firstName": "Isaac",
    "lastName": "Brock",
    "email": "isaac.brock@example.com",
    "login": "isaac.brock@example.com",
    "mobilePhone": "555-415-1337"
  },
  "credentials": {
    "password" : { "value": "tlpWENT2m" }
  }
}' "https://${org}.okta.com/api/v1/users?activate=false"

这是使用 Curl

使用 Python 中的 requests library:

import requests

url = '{{org}}.okta.com/api/v1/users'

headers = {
  'accept': 'application/json',
  'authorization' : 'SSWS {{api_token}}',
  'content-type': 'application/json'
}

body = {
  'profile': {
    'firstName': 'Isaac',
    'lastName': 'Brock',
    'email': 'isaac@{{email_suffix}}',
    'login': 'isaac@{{email_suffix}}'
  },
  'credentials': {
    'password' : { 'value': '{{password}}' }
  }
}

r = requests.post(url, headers=headers, json=body)
# r.json

使用Okta Python SDK,需要先创建client,然后调用create_user()方法

from okta import UsersClient
from okta.models.user import User

usersClient = UsersClient("https://{{org}}.okta.com", "{{api_token}}")

user = User(login='isaac@{{email_suffix}}',
            email='isaac@{{email_suffix}}',
            firstName='Isacc',
            lastName='Brock')

user = usersClient.create_user(user, activate=False)