你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于 Python 的 Azure 通信标识包客户端库 - 版本 1.4.0
Azure 通信标识客户端包旨在用于设置使用 Azure 通信服务产品/服务的基础知识。 此包有助于创建标识用户令牌,供其他客户端包(如聊天、呼叫、短信)使用。
源代码 | 包 (Pypi) | 包 (Conda) | API 参考文档 | 产品文档
免责声明
对 Python 2.7 的 Azure SDK Python 包支持已于 2022 年 1 月 1 日结束。 有关详细信息和问题,请参阅 https://github.com/Azure/azure-sdk-for-python/issues/20691
入门
先决条件
- 使用此包需要 Python 3.7 或更高版本。
- 必须具有 Azure 订阅
- 已部署的通信服务资源。 可以使用 Azure 门户或Azure PowerShell进行设置。
安装包
使用 pip 安装适用于 Python 的 Azure 通信标识客户端库:
pip install azure-communication-identity
关键概念
CommunicationIdentityClient
CommunicationIdentityClient
提供操作以实现以下目的:
创建/删除Azure 通信服务中使用的标识。 这些标识可用于利用 Azure 通信产品/服务,并且可以通过令牌范围限定为具有有限能力。
创建/撤销作用域内用户访问令牌,以访问聊天、呼叫、短信等服务。 令牌是为有效的 Azure 通信标识颁发的,可以随时撤销。
初始化标识客户端
# You can find your endpoint and access token from your resource in the Azure Portal
import os
from azure.communication.identity import CommunicationIdentityClient
from azure.identity import DefaultAzureCredential
connection_str = "endpoint=ENDPOINT;accessKey=KEY"
endpoint = "https://<RESOURCE_NAME>.communication.azure.com"
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
identity_client_managed_identity = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
#You can also authenticate using your connection string
identity_client = CommunicationIdentityClient.from_connection_string(connection_str)
示例
以下部分提供了几个代码片段,涵盖了一些最常见的Azure 通信服务任务,包括:
创建新用户
create_user
使用 方法创建新用户。
user = identity_client.create_user()
print("User created with id:" + user.properties['id'])
为用户颁发或刷新访问令牌
get_token
使用 方法为用户颁发或刷新作用域内访问令牌。
将用户对象作为参数传入,并传入 列表 CommunicationTokenScope
。 范围选项包括:
CHAT
(使用此权限可完全访问聊天 API)VOIP
(使用此方法来完全访问调用 API)CHAT_JOIN
(访问聊天 API,但无权创建、删除或更新聊天线程)CHAT_JOIN_LIMITED
(不允许添加或删除参与者的CHAT_JOIN的受限版本)VOIP_JOIN
(访问呼叫 API,但未授权启动新调用)
tokenresponse = identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT])
print("Token issued with value: " + tokenresponse.token)
为用户颁发或刷新具有自定义过期期限的访问令牌
可以指定令牌的过期时间。 可以将令牌配置为在 1 小时或 24 小时内过期。 默认过期时间为 24 小时。
token_expires_in = timedelta(hours=1)
tokenresponse = identity_client.get_token(user, scopes=[CommunicationTokenScope.CHAT], token_expires_in=token_expires_in)
print("Token issued with value: " + tokenresponse.token)
在单个请求中创建用户和令牌
为方便起见,请使用 create_user_and_token
创建新用户,并通过一个函数调用颁发令牌。 这转换为单个 Web 请求,而不是先创建用户,然后颁发令牌。
user, tokenresponse = identity_client.create_user_and_token(scopes=[CommunicationTokenScope.CHAT])
print("User id:" + user.properties['id'])
print("Token issued with value: " + tokenresponse.token)
在单个请求中创建具有自定义过期时间的用户和令牌
可以指定令牌的过期时间。 可以将令牌配置为在 1 小时或 24 小时内过期。 默认过期时间为 24 小时。
token_expires_in = timedelta(hours=1)
user, tokenresponse = identity_client.create_user_and_token(scopes=[CommunicationTokenScope.CHAT], token_expires_in=token_expires_in)
print("User id:" + user.properties['id'])
print("Token issued with value: " + tokenresponse.token)
撤销用户的访问令牌
用于 revoke_tokens
撤销用户的所有访问令牌。 将用户对象作为参数传入
identity_client.revoke_tokens(user)
删除用户
delete_user
使用 方法删除用户。 将用户对象作为参数传入
identity_client.delete_user(user)
将 Teams 用户的 Azure AD 访问令牌交换为通信标识访问令牌
get_token_for_teams_user
使用 方法将 Teams 用户的 Azure AD 访问令牌交换为新的通信标识访问令牌。
identity_client.get_token_for_teams_user(aad_token, client_id, user_object_id)
疑难解答
Azure 通信服务标识客户端将引发 Azure Core 中定义的异常。
后续步骤
更多示例代码
请查看 示例 目录,获取有关如何使用此库管理标识和令牌的详细示例。
提供反馈
如果遇到任何 bug 或有建议,请在项目的“ 问题 ”部分中提出问题
贡献
本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 https://cla.microsoft.com 。
提交拉取请求时,CLA 机器人将自动确定你是否需要提供 CLA,并相应地修饰 PR(例如标签、注释)。 直接按机器人提供的说明操作。 只需使用 CLA 对所有存储库执行一次这样的操作。
此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈