Microsoft Azure Communication Identity Service Python客户端库
项目描述
Azure Communication Identity Package客户端库,用于Python
Azure Communication Identity客户端包旨在用于设置使用Azure Communication Service提供的必备条件。此包有助于创建身份用户令牌,供其他客户端包(如聊天、通话、短信)使用。
源代码 | 包(Pypi) | 包(Conda) | API参考文档 | 产品文档
免责声明
Azure SDK Python包对Python 2.7的支持已于2022年1月1日结束。有关更多信息及疑问,请参阅https://github.com/Azure/azure-sdk-for-python/issues/20691
入门
先决条件
- 使用此包需要Python 3.7或更高版本。
- 您必须拥有一个Azure订阅
- 已部署的Communication Services资源。您可以使用Azure Portal或Azure PowerShell来设置。
安装包
使用
pip
安装Azure Communication Identity客户端库pip install azure-communication-identity
关键概念
CommunicationIdentityClient
CommunicationIdentityClient
提供以下操作:
-
创建/删除在Azure Communication Services中使用的身份。这些身份可用于利用Azure Communication服务,并通过令牌作用域限制能力。
-
创建/撤销用于访问聊天、通话、短信等服务的作用域访问令牌。令牌为有效的Azure Communication身份颁发,并可随时撤销。
初始化身份客户端
# 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 Communication Services的一些常见任务,包括
创建新用户
使用
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
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)
交换团队用户的Azure AD访问令牌和通信身份访问令牌
使用
get_token_for_teams_user
identity_client.get_token_for_teams_user(aad_token, client_id, user_object_id)
故障排除
Azure Communication Service身份客户端将引发在
中定义的异常。下一步
更多示例代码
请参阅
目录,了解如何使用此库管理身份和令牌的详细示例。提供反馈
如果您遇到任何错误或有建议,请在项目的
部分提交问题贡献
本项目欢迎贡献和建议。大多数贡献都需要您同意贡献者许可协议(CLA),声明您有权并且实际上确实授权我们使用您的贡献。有关详细信息,请访问 https://cla.microsoft.com。
当您提交拉取请求时,CLA机器人会自动确定您是否需要提供CLA,并适当装饰PR(例如,标签,注释)。只需遵循机器人提供的说明。您只需在整个使用我们CLA的所有仓库中这样做一次。
本项目已采用微软开源行为准则。有关更多信息,请参阅行为准则常见问题解答或通过opencode@microsoft.com联系,如有任何额外问题或评论。
项目详情
azure-communication-identity-1.5.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | d3186403395b78066ff30313ac119693694d2da9e0c76e9ac70eaa590dcb29e8 |
|
MD5 | 5f4832a49fe679cc1879aada98ec69e0 |
|
BLAKE2b-256 | f69e6cbc94f5c01406083960bea219ee9d9313ad925c894b6b5df3df1d3d51a0 |
azure_communication_identity-1.5.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | dd6dc7aafc9f3707ffbbf40a45f2b4ccb5dc67f4d1545e74b083f83f9afa7a1a |
|
MD5 | f246d4331dc682dd209c8f7740f8b012 |
|
BLAKE2b-256 | b1219266411a36a182e235ad9ed338a071e417d01d6804747ae959c2232399b0 |