Package: azure-communication-identity
Azure Communication Identity Package client library for PythonAzure Communication Identity client package is intended to be used to setup the basics for opening a way to use Azure Communication Service offerings. This package helps to create identity user tokens to be used by other client packages such as chat, calling, sms.Source code|Package (Pypi)|Package (Conda)|API reference documentation|Product documentationDisclaimerAzure SDK Python packages support for Python 2.7 has ended 01 January 2022. For more information and questions, please refer tohttps://github.com/Azure/azure-sdk-for-python/issues/20691Getting startedPrerequisitesPython 3.7 or later is required to use this package.You must have anAzure subscriptionA deployed Communication Services resource. You can use theAzure Portalor theAzure PowerShellto set it up.Install the packageInstall the Azure Communication Identity client library for Python withpip:pipinstallazure-communication-identityKey conceptsCommunicationIdentityClientCommunicationIdentityClientprovides operations for:Create/delete identities to be used in Azure Communication Services. Those identities can be used to make use of Azure Communication offerings and can be scoped to have limited abilities through token scopes.Create/revoke scoped user access tokens to access services such as chat, calling, sms. Tokens are issued for a valid Azure Communication identity and can be revoked at any time.Initializing Identity Client# You can find your endpoint and access token from your resource in the Azure Portalimportosfromazure.communication.identityimportCommunicationIdentityClientfromazure.identityimportDefaultAzureCredentialconnection_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 stringidentity_client=CommunicationIdentityClient.from_connection_string(connection_str)ExamplesThe following section provides several code snippets covering some of the most common Azure Communication Services tasks, including:Creating a new userIssuing or Refreshing an access token for a userCreating a user and a token in a single requestRevoking a user's access tokensDeleting a userExchanging Azure AD access token of a Teams User for a Communication Identity access tokenCreating a new userUse thecreate_usermethod to create a new user.user=identity_client.create_user()print("User created with id:"+user.properties['id'])Issuing or Refreshing an access token for a userUse theget_tokenmethod to issue or refresh a scoped access token for the user.Pass in the user object as a parameter, and a list ofCommunicationTokenScope. Scope options are:CHAT(Use this for full access to Chat APIs)VOIP(Use this for full access to Calling APIs)CHAT_JOIN(Access to Chat APIs but without the authorization to create, delete or update chat threads)CHAT_JOIN_LIMITED(A more limited version of CHAT_JOIN that doesn't allow to add or remove participants)VOIP_JOIN(Access to Calling APIs but without the authorization to start new calls)tokenresponse=identity_client.get_token(user,scopes=[CommunicationTokenScope.CHAT])print("Token issued with value: "+tokenresponse.token)Issuing or Refreshing an access token with custom expiration for a userYou can specify expiration time for the token. The token can be configured to expire in as little as one hour or as long as 24 hours. The default expiration time is 24 hours.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)Creating a user and a token in a single requestFor convenience, usecreate_user_and_tokento create a new user and issue a token with one function call. This translates into a single web request as opposed to creating a user first and then issuing a 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)Creating a user and a token with custom expiration in a single requestYou can specify expiration time for the token. The token can be configured to expire in as little as one hour or as long as 24 hours. The default expiration time is 24 hours.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)Revoking a user's access tokensUserevoke_tokensto revoke all access tokens for a user. Pass in the user object as a parameteridentity_client.revoke_tokens(user)Deleting a userUse thedelete_usermethod to delete a user. Pass in the user object as a parameteridentity_client.delete_user(user)Exchanging Azure AD access token of a Teams User for a Communication Identity access tokenUse theget_token_for_teams_usermethod to exchange an Azure AD access token of a Teams User for a new Communication Identity access token.identity_client.get_token_for_teams_user(aad_token,client_id,user_object_id)TroubleshootingThe Azure Communication Service Identity client will raise exceptions defined inAzure Core.Next stepsMore sample codePlease take a look at thesamplesdirectory for detailed examples of how to use this library to manage identities and tokens.Provide FeedbackIf you encounter any bugs or have suggestions, please file an issue in theIssuessection of the projectContributingThis project welcomes contributions and suggestions.  Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visithttps://cla.microsoft.com.When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the
PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.This project has adopted theMicrosoft Open Source Code of Conduct.
For more information see theCode of Conduct FAQor contactopencode@microsoft.comwith any additional questions or comments.