Authing 文档
文档
快速开始
概念
使用指南
开发集成
应用集成
加入 APN
多租户(内测版)
旧版
快速开始
概念
使用指南
开发集成
应用集成
加入 APN
多租户(内测版)
旧版
开发集成
  • 登录组件 (Guard)
  • 单点登录(SSO)
  • JavaScript / Node.js
  • Java / Kotlin
  • Python
  • C#
  • PHP
  • Go
  • Ruby
  • Android
  • iOS
  • Swift
  • Flutter
  • React Native
  • 微信小程序
  • 微信网页授权
  • 框架集成
  • 错误代码
  1. 开发集成
  2. /
  3. Python

¶ Authing - Python

更新时间: 2022-03-11 11:21:01

Authing Python SDK 由两部分组成:ManagementClient 和 AuthenticationClient。

AuthenticationClient 以终端用户(End User)的身份进行请求,提供了登录、注册、登出、管理用户资料、获取授权资源等所有管理用户身份的方法;此模块还提供了各种身份协议的 SDK,如 OpenID Connect, OAuth 2.0, SAML 和 CAS。此模块适合用于纯后端交互的服务器环境。

ManagementClient 以管理员(Administrator)的身份进行请求,用于管理用户池资源和执行管理任务,提供了管理用户、角色、应用、资源等方法;一般来说,你在 Authing 控制台 (opens new window) 中能做的所有操作,都能用此模块完成。此模块适合在后端服务器环境使用。

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

Authing Python SDK 同时支持 python2 和 python3。

¶ GitHub 下载地址

条目 说明
支持版本 Python 2.7 +
仓库地址 https://github.com/Authing/authing-py-sdk (opens new window)

¶ 安装

pip install authing

¶ 使用管理模块

ManagementClient 以管理员(Administrator)的身份进行请求,用于管理用户池资源和执行管理任务,提供了管理用户、角色、应用、资源等方法;一般来说,你在 Authing 控制台 (opens new window) 中能做的所有操作,都能用此模块完成。此模块适合在后端服务器环境使用。

¶ 初始化

ManagementClient 初始化需要传入用户池 ID userPoolId 和用户池密钥 secret:

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

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

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 以终端用户(End User)的身份进行请求,提供了登录、注册、登出、管理用户资料、获取授权资源等所有管理用户身份的方法;此模块还提供了各种身份协议的 SDK,如 OpenID Connect, OAuth 2.0, SAML 和 CAS。此模块适合用于纯后端交互的服务器环境。

¶ 初始化

初始化 AuthenticationClient 需要 app_id(应用 ID)和 app_host(应用端点,如 https://YOUR_DOMAIN.authing.cn):

你可以在此获取 app_id 和 app_host。

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    app_id='AUTHING_APP_ID',
    app_host='https://YOUR_DOMAIN.authing.cn'
))

完整参数如下:

  • app_id: Authing 应用 ID(必填);
  • app_host: Authing 应用地址(必填),格式为 https://YOUR_DOMAIN.authing.cn;
  • token: 用户的 id_token(可选),你可以在前端 localStorage 中缓存用户 id_token,然后使用 id_token 初始化 SDK,从而实现记住登录的目的;
  • timeout: 请求超时时间(可选),位为毫秒,默认为 10000(10 秒);
  • on_error: 错误处理函数(可选),你可以用其来全局捕捉 Authing 客户端请求的所有异常。完整的错误代码请见此文档。函数定义为:
def on_error(code, message):
    raise AuthingException(code=code, errmsg=message)
  • enc_public_key: 密码非对称加密公钥(可选),如果你使用的是 Authing 公有云服务,可以忽略;如果你使用的是私有化部署的 Authing,请联系 Authing IDaaS 服务管理员。
  • lang: 接口 Message 返回语言格式(可选),可选值为 zh-CN 和 en-US,默认为 zh-CN。

¶ 快速开始

我们推荐每次请求初始化一个新的 AuthenticationClient,保证不同请求之间完全隔离。

username = "bob"
password = "passw0rd"
user = authentication_client.login_by_username(
    username=username,
    password=password,
)

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

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

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

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    app_id='AUTHING_APP_ID',
    app_host='https://YOUR_DOMAIN.authing.cn',
    token='ID_TOKEN'
))

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

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

¶ 错误处理

如果函数执行失败会抛出异常,你需要使用 try/except 捕捉异常:

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) # 用户不存在

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

¶ 私有化部署

私有化部署场景需要指定你私有化的 Authing 服务的 GraphQL 端点(不带协议头和 Path)和密码加密公钥,如果你不清楚可以联系 Authing IDaaS 服务管理员。

from authing.v2.management import ManagementClient, ManagementClientOptions

management_client = ManagementClient(
  options=ManagementClientOptions(
    user_pool_id='AUTHING_USERPOOL_ID',
    secret='AUTHING_USERPOOL_SECRET',
    host="https://core.you-authing-service.com",
    enc_public_key="YOUR_PUBLIC_KEY"
))

¶ 获取帮助

Join us on forum: #authing-chat (opens new window)

上一篇: 管理多租户 下一篇: 用户认证模块

本文是否有解决您的问题?

如果遇到其他问题,你可以在 authing-chat/community 联系我们。

  • GitHub 下载地址
  • 安装
  • 使用管理模块
  • 使用认证模块
  • 错误处理
  • 私有化部署
  • 获取帮助

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
GitHub (opens new window)
社区用户中心 (opens new window)

公司

服务状态
15559944612
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司