华为云会议部门及用户管理典型场景_Python
引导式阅读
Python
华为云会议部门及用户管理典型场景_Python
作者
C***
上架时间
2023-11-16 06:28:15

华为云会议部门及用户管理典型场景_Python

0. 版本说明

本示例基于华为云SDK V3.0版本开发。

1.介绍

华为云会议部门及用户管理介绍

华为云会议的Pyton SDK(huaweicloudsdkmeeting)开放了企业部门和用户管理接口,用于与第三方的业务系统集成。可以实现企业部门及用户的增、删、改、查。支持如下功能:

功能 方法名
添加部门 add_department
修改部门 update_department
删除部门 delete_department
查询部门及其一级子部门列表 show_dept_and_child_dept
按名称查询所有部门 search_department_by_name
添加用户 add_user
修改用户 update_user
查询用户详情 show_user_detail
批量删除用户 batch_delete_users
批量修改用户状态 batch_update_user_status
用户查询自己的信息 show_my_info
用户修改自己的信息 update_my_info
邀请用户 invite_user

2.开发前准备

  • 申请华为云账号
  • 申请华为云会议测试资源
  • 获取华为云会议的管理员帐号和密码

具体操作步骤,请参考:开发前准备

3.问题求助通道:

4.安装SDK:

在使用华为云会议服务端SDK前,您需要安装“huaweicloudsdkcore”和“huaweicloudsdkmeeting”。

pip install huaweicloudsdkcore pip install huaweicloudsdkmeeting

5. 开始使用

接口调用概览

图1 华为云会议部门及用户管理典型场景的接口调用时序图

步骤1:创建SDK client

描述

根据华为云会议管理员帐号和密码创建SDK client。 说明:

  • 华为云会议不是使用IMA的AK、SK方式鉴权,需要单独申请华为云会议的管理员帐号。
  • 中国站的endpoint是api.meeting.huaweicloud.com;国际站的endpoint是api-intl.meeting.huaweicloud.com
示例代码
# coding: utf-8 from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from huaweicloudsdkmeeting.v1.meeting_credentials import MeetingCredentials import os def create_client(): try: # 认证用的账号和密码直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; # 本示例以账号和密码保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量YOUR_MANAGER_ACCOUNT和YOUR_PASSWORD。 username = os.environ["YOUR_MANAGER_ACCOUNT"] password = os.environ["YOUR_PASSWORD"] credentials = MeetingCredentials(username, password) client = MeetingClient.new_builder() \ .with_credentials(credentials) \ .with_endpoint("api.meeting.huaweicloud.com") \ .build() return client except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)

步骤2:在指定的部门目录下创建新的目录

描述

使用步骤1生成的client中的search_dept_by_id方法和add_department方法,先查询父部门的部门ID,在这个部门ID下新创建部门。

示例代码
# coding: utf-8 from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from create_client import create_client def search_dept_by_id(client=None, dept_id=None): try: request = ShowDeptAndChildDeptRequest(dept_code=dept_id) response = client.show_dept_and_child_dept(request) print(response) return response.dept_name except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) if __name__ == "__main__": client = create_client() # 企业的根部门的ID固定是1 dept_name = search_dept_by_id(client, "1") print(dept_name)
# coding: utf-8 from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from create_client import create_client from search_dept_by_name import search_dept_by_name from search_dept_by_id import search_dept_by_id def add_corp_dept(client=None, parent_dept_name=None, new_dept_name=None): dept_code = search_dept_by_name(client, parent_dept_name) try: if dept_code != 0: body = DeptDTO(dept_name=new_dept_name, parent_dept_code=dept_code) request = AddDepartmentRequest(body=body) response = client.add_department(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) if __name__ == "__main__": client = create_client() dept_name = search_dept_by_id(client, "1") add_corp_dept(client, dept_name, "开发部") add_corp_dept(client, "开发部", "开发1部") add_corp_dept(client, "开发部", "开发2部") add_corp_dept(client, "开发部", "开发3部") add_corp_dept(client, dept_name, "财务部") add_corp_dept(client, dept_name, "法务部")

步骤3:在指定部门下创建用户

描述

根据部门名称找到对应的部门ID,在对应不部门下创建新用户。

示例代码
# coding: utf-8 from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from create_client import create_client from search_dept_by_name import search_dept_by_name def add_user(client=None, dept_name=None, user_name=None, account=None, phone=None, email=None): if dept_name is not None: dept_code = search_dept_by_name(client, dept_name) try: body = AddUserDTO(name=user_name, dept_code=dept_code, phone=phone, email=email) request = AddUserRequest(body=body) response = client.add_user(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) if __name__ == "__main__": client = create_client() add_user(client, "开发1部", "张三", "dev001@testcorp.com", "13912345678", "dev001@testcorp.com") add_user(client, "财务部", "张三", "finacial001@testcorp.com", "13912345690", "finacial001@testcorp.com") add_user(client, "法务部", "张三", "legal001@testcorp.com", "13912345670", "legal001@testcorp.com")

步骤4:删除指定姓名的帐号

描述

根据姓名查找对应的account,再删除这些帐号。

示例代码
from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from create_client import create_client def search_corp_dir(client=None, search_key=None): try: request = SearchCorpDirRequest(search_key=search_key) response = client.search_corp_dir(request) return response except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) if __name__ == "__main__": client = create_client() response = search_corp_dir(client) print(response)
# coding: utf-8 from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkmeeting.v1 import * from create_client import create_client from search_corp_dir import search_corp_dir def delete_user(client=None, user_list=None): try: if len(user_list) != 0: request = BatchDeleteUsersRequest(body=user_list) response = client.batch_delete_users(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg) if __name__ == "__main__": client = create_client() result = search_corp_dir(client, "张三") account_list = [] for user in result.data: account_list.append(user.account) delete_user(client, account_list) print("delete " + str(len(account_list)) + " accounts")

6.参考文件

7.修订记录

发布日期 文档版本 修订说明
2021-07-31 1.0 文档首次发布