Skip to main content

Authing SDK for Python

Project description

Authing - Python

Authing Python SDK 由两部分组成:ManagementClientAuthenticationClientManagementClient 中进行的所有操作均以管理员的身份进行,包含管理用户、管理角色、管理权限策略、管理用户池配置等模块。AuthenticationClient 中的所有操作以当前终端用户的身份进行,包含登录、注册、修改用户资料、退出登录等方法。

你应该将初始化过后的 ManagementClient 实例设置为一个全局变量(只初始化一次),而 AuthenticationClient 应该每次请求初始化一个。

Authing Python SDK 同时支持 python2python3

安装

pip install authing

使用用户管理模块

初始化 ManagementClient 需要 user_pool_id(用户池 ID) 和 secret(用户池密钥):

你可以在此了解如何获取 UserPoolId 和 Secret .

from authing.v2.management import ManagementClient, ManagementClientOptions

management_client = ManagementClient(
  options=ManagementClientOptions(
    user_pool_id='AUTHING_USERPOOL_ID',
    secret='AUTHING_USERPOOL_SECRET',
))

现在 ManagementClient() 实例就可以使用了。例如可以获取用户池中的用户列表:

data = management_client.users.list()

返回的数据如下:

{
  "totalCount": 1,
  "list": [
    {
      "id": "5f7ddfe62ba819802422362e",
      "arn": "arn:cn:authing:5f7a993eb9b49dcd5c021e40:user:5f7ddfe62ba819802422362e",
      "userPoolId": "5f7a993eb9b49dcd5c021e40",
      "username": "nhxcpzmklk",
      "email": null,
      "emailVerified": false,
      "phone": null,
      "phoneVerified": false,
      "unionid": null,
      "openid": null,
      "nickname": null,
      "registerSource": [
        "import:manual"
      ],
      "photo": "https://usercontents.authing.cn/authing-avatar.png",
      "password": "a56f21e5659428f9b353be4ed667fc05",
      "oauth": null,
      "token": null,
      "tokenExpiredAt": null,
      "loginsCount": 0,
      "lastLogin": null,
      "lastIP": null,
      "signedUp": "2020-10-07T23:33:58+08:00",
      "blocked": false,
      "isDeleted": false,
      "device": null,
      "browser": null,
      "company": null,
      "name": null,
      "givenName": null,
      "familyName": null,
      "middleName": null,
      "profile": null,
      "preferredUsername": null,
      "website": null,
      "gender": "U",
      "birthdate": null,
      "zoneinfo": null,
      "locale": null,
      "address": null,
      "formatted": null,
      "streetAddress": null,
      "locality": null,
      "region": null,
      "postalCode": null,
      "country": null,
      "createdAt": "2020-10-07T23:33:58+08:00",
      "updatedAt": "2020-10-07T23:33:58+08:00",
    }
  ]
}

使用用户认证模块

初始化 AuthenticationClient 需要 user_pool_id(用户池 ID):

你可以在此了解如何获取 UserPoolId .

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    user_pool_id='AUTHING_USERPOOL_ID'
))

接下来可以进行注册登录等操作:

username = get_random_string(10)
password = get_random_string(10)
user = authentication_client.login_by_username(
    username=username,
    password=password,
)

完成登录之后,update_profile 等要求用户登录的方法就可用了:

authentication_client.update_profile({
  'nickname': 'Nick'
})

你也可以使用 access_token 参数来初始化 AuthenticationClient, 而不需要每次都调用 login 方法:

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    user_pool_id='AUTHING_USERPOOL_ID',
    access_token='AUTHING_USER_TOKEN'
))

再次执行 update_profile 方法,发现也成功了:

user = authentication_client.update_profile({
  'nickname': 'Nick'
})

错误处理

from authing.v2.exceptions import AuthingException

try:
    authentication_client.login_by_username(
        username='bob',
        password='passw0rd',
    )
except AuthingException as e:
    print(e.code) # 2004
    print(e.message) # 用户不存在

完整的错误代码请见此文档

获取帮助

Join us on Gitter: #authing-chat

接口索引

详细的接口文档请见:认证模块文档管理模块文档

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

authing-4.2.0.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

authing-4.2.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

Details for the file authing-4.2.0.tar.gz.

File metadata

  • Download URL: authing-4.2.0.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for authing-4.2.0.tar.gz
Algorithm Hash digest
SHA256 4d46eb6ca34106fd8d6e662376aeac8d69cceabf33e78c4591bf4166ff12527f
MD5 895c188ab738b808bdabe7037b739271
BLAKE2b-256 f8421292abb7861e9649abf78334817786bd9e62c8908832a539703ea67b6d55

See more details on using hashes here.

File details

Details for the file authing-4.2.0-py3-none-any.whl.

File metadata

  • Download URL: authing-4.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.9.1

File hashes

Hashes for authing-4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7cd145abf7c7883c40983c1265264cf5cb66c5840379e5fc35df6fd9b79fcc72
MD5 a4ffc4ee4423830754ac3d02f9f89230
BLAKE2b-256 cb5415a8160de614c792a9154d27c52e723a7ad2abc7ead9cb4044a34ec5b92d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page