Python 用 Azure Communication Identity Package クライアント ライブラリ - バージョン 1.4.0

Azure Communication Identity クライアント パッケージは、Azure Communication Service オファリングを使用する方法を開くための基本を設定するために使用することを目的としています。 このパッケージは、チャット、通話、SMS などの他のクライアント パッケージで使用される ID ユーザー トークンを作成するのに役立ちます。

ソースコード | パッケージ (Pypi) | パッケージ (Conda) | API リファレンス ドキュメント | 製品ドキュメント

免責事項

Python 2.7 の Azure SDK Python パッケージのサポートは、2022 年 1 月 1 日に終了しました。 詳細と質問については、https://github.com/Azure/azure-sdk-for-python/issues/20691 を参照してください

作業の開始

前提条件

パッケージをインストールする

pip を使用して Python 用 Azure Communication Identity クライアント ライブラリをインストールします。

pip install azure-communication-identity

主要な概念

CommunicationIdentityClient

CommunicationIdentityClient には、以下を目的とした操作が用意されています。

  • Azure Communication Servicesで使用する ID を作成または削除します。 これらの ID は、Azure Communication オファリングを利用するために使用でき、トークン スコープを通じて制限された機能を持つスコープにすることができます。

  • チャット、通話、SMS などのサービスにアクセスするためのスコープ付きユーザー アクセス トークンを作成または取り消します。 トークンは有効な Azure Communication ID に対して発行され、いつでも取り消すことができます。

ID クライアントの初期化

# 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)

1 つの要求でユーザーとトークンを作成する

便宜上、 を使用 create_user_and_token して新しいユーザーを作成し、1 つの関数呼び出しでトークンを発行します。 これは、最初にユーザーを作成してからトークンを発行するのではなく、単一の 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 つの要求でカスタム有効期限を持つユーザーとトークンを作成する

トークンの有効期限を指定できます。 トークンは、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 アクセス トークンをコミュニケーション ID アクセス トークンと交換する

メソッドを get_token_for_teams_user 使用して、Teams ユーザーの Azure AD アクセス トークンを新しい Communication Identity アクセス トークンと交換します。

identity_client.get_token_for_teams_user(aad_token, client_id, user_object_id)

トラブルシューティング

Azure Communication Service Identity クライアントは、 Azure Core で定義されている例外を発生させます。

次のステップ

その他のサンプル コード

このライブラリを使用して ID とトークンを管理する方法の詳細な例については、 サンプル ディレクトリを参照してください。

フィードバックの提供

バグが発生した場合、または提案がある場合は、プロジェクトの [問題 ] セクションに問題を報告してください

共同作成

このプロジェクトでは、共同作成と提案を歓迎しています。 ほとんどの共同作成では、共同作成者使用許諾契約書 (CLA) にご同意いただき、ご自身の共同作成内容を使用する権利を Microsoft に供与する権利をお持ちであり、かつ実際に供与することを宣言していただく必要があります。 詳細については、 https://cla.microsoft.com を参照してください。

pull request を送信すると、CLA を提供して PR (ラベル、コメントなど) を適宜装飾する必要があるかどうかを CLA ボットが自動的に決定します。 ボットによって提供される手順にそのまま従ってください。 この操作は、Microsoft の CLA を使用するすべてのリポジトリについて、1 回だけ行う必要があります。

このプロジェクトでは、Microsoft オープン ソースの倫理規定を採用しています。 詳しくは、「Code of Conduct FAQ (倫理規定についてよくある質問)」を参照するか、opencode@microsoft.com 宛てに質問またはコメントをお送りください。