跳转到主要内容

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

入门

先决条件

安装包

使用

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

在单个函数调用中创建新用户和颁发令牌。这相当于单个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)

交换团队用户的Azure AD访问令牌和通信身份访问令牌

使用

get_token_for_teams_user

方法将团队用户的Azure AD访问令牌交换为新的通信身份访问令牌。

identity_client.get_token_for_teams_user(aad_token, client_id, user_object_id)

故障排除

Azure Communication Service身份客户端将引发在

Azure Core

中定义的异常。

下一步

更多示例代码

请参阅

samples

目录,了解如何使用此库管理身份和令牌的详细示例。

提供反馈

如果您遇到任何错误或有建议,请在项目的

Issues

部分提交问题

贡献

本项目欢迎贡献和建议。大多数贡献都需要您同意贡献者许可协议(CLA),声明您有权并且实际上确实授权我们使用您的贡献。有关详细信息,请访问 https://cla.microsoft.com

当您提交拉取请求时,CLA机器人会自动确定您是否需要提供CLA,并适当装饰PR(例如,标签,注释)。只需遵循机器人提供的说明。您只需在整个使用我们CLA的所有仓库中这样做一次。

本项目已采用微软开源行为准则。有关更多信息,请参阅行为准则常见问题解答或通过opencode@microsoft.com联系,如有任何额外问题或评论。

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分布

azure-communication-identity-1.5.0.tar.gz (66.7 kB 查看哈希值)

上传时间 源代码

构建分布

azure_communication_identity-1.5.0-py3-none-any.whl (65.6 kB 查看哈希值)

上传时间 Python 3

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面