Package: azure-mixedreality-authentication
Azure Mixed Reality Authentication Package client library for PythonMixed Reality services, like Azure Spatial Anchors, Azure Remote Rendering, and others, use the Mixed Reality security
token service (STS) for authentication. This package supports exchanging Mixed Reality account credentials for an access
token from the STS that can be used to access Mixed Reality services.Getting startedCurrently supported environmentsThis package has been tested with Python 2.7, 3.5, 3.6, 3.7, 3.8, and 3.9.PrerequisitesAnAzure subscription.You must have an account with anAzure Mixed Reality service:Azure Remote RenderingAzure Spatial AnchorsFamiliarity with the authentication and credential concepts from theAzure Identity library.Python 2.7, or 3.5 or later is required to use this package.Install the packageInstall the Azure Mixed Reality Authentication SDK.pipinstall--preazure-mixedreality-authenticationCreate and authenticate aMixedRealityStsClientTo create a client object to request an access token for a Mixed Reality service, you will need theaccount identifierandaccount domainof your Mixed Reality service resource and acredential.Mixed Reality services support a few different forms of authentication:Account Key authenticationAccount keys enable you to get started quickly with using Mixed Reality services. But before you deploy your application
to production, we recommend that you update your app to use Azure AD authentication.Azure Active Directory (AD) token authenticationIf you're building an enterprise application and your company is using Azure AD as its identity system, you can use
user-based Azure AD authentication in your app. You then grant access to your Mixed Reality accounts by using your
existing Azure AD security groups. You can also grant access directly to users in your organization.Otherwise, we recommend that you obtain Azure AD tokens from a web service that supports your app. We recommend this
method for production applications because it allows you to avoid embedding the credentials for access to a Mixed
Reality service in your client application.Seeherefor detailed instructions and information.Using account key authenticationUse theAzure Portalto browse to your Mixed Reality service resource and retrieve anaccount key.Once you have an account key, you can use theAzureKeyCredentialclass to authenticate the client as follows:fromazure.core.credentialsimportAzureKeyCredentialfromazure.mixedreality.authenticationimportMixedRealityStsClientaccount_id="<ACCOUNTD ID>"account_domain="<ACCOUNT_DOMAIN>"account_key="<ACCOUNT_KEY>"key_credential=AzureKeyCredential(account_key)client=MixedRealityStsClient(account_id,account_domain,key_credential)Note: Account key authentication isnot recommendedfor production applications.Using an Azure Active Directory CredentialAccount key authentication is used in most of the examples, but you can also authenticate with Azure Active Directory
using theAzure Identity library. This is the recommended method for production applications. To use
theDefaultAzureCredentialprovider shown below, or other credential providers provided with
the Azure SDK, please install the@azure/identitypackage:You will also need toregister a new AAD applicationand grant access to your Mixed Reality resource
by assigning the appropriate role for your Mixed Reality service to your service principal.fromazure.identityimportDefaultAzureCredentialfromazure.mixedreality.authenticationimportMixedRealityStsClientaccount_id="<ACCOUNTD ID>"account_domain="<ACCOUNT_DOMAIN>"default_credential=DefaultAzureCredential()client=MixedRealityStsClient(account_id,account_domain,default_credential)Key conceptsMixedRealityStsClientTheMixedRealityStsClientis the client library used to access the Mixed Reality STS to get an access token. An access
token can be retrieved by callingget_token()on anMixedRealityStsClientinstance.Tokens obtained from the Mixed Reality STS have a lifetime of24 hours.Token result valueThe return value for a successful call toget_tokenis anazure.core.credentials.AccessToken.See the authentication examplesaboveorAzure Identityfor more complex
authentication scenarios.Retrieve an access token synchronouslyfromazure.core.credentialsimportAzureKeyCredentialfromazure.mixedreality.authenticationimportMixedRealityStsClientaccount_id="<ACCOUNTD ID>"account_domain="<ACCOUNT_DOMAIN>"account_key="<ACCOUNT_KEY>"key_credential=AzureKeyCredential(account_key)client=MixedRealityStsClient(account_id,account_domain,key_credential)token=client.get_token()Retrieve an access token asynchronouslyfromazure.core.credentialsimportAzureKeyCredentialfromazure.mixedreality.authentication.aioimportMixedRealityStsClientaccount_id="<ACCOUNTD ID>"account_domain="<ACCOUNT_DOMAIN>"account_key="<ACCOUNT_KEY>"key_credential=AzureKeyCredential(account_key)client=MixedRealityStsClient(account_id,account_domain,key_credential)token=awaitclient.get_token()ExamplesThese are code samples that show common scenario operations with the Azure Mixed Reality Authentication client library.
The async versions of the samples (the python sample files appended with_async) show asynchronous operations,
and require Python 3.5 or later.
Before running the sample code, refer to Prerequisitesto create a resource, then set some Environment VariablessetMIXEDREALITY_ACCOUNT_DOMAIN="<the Mixed Reality account domain>"setMIXEDREALITY_ACCOUNT_ID="<the Mixed Reality account identifier>"setMIXEDREALITY_ACCOUNT_KEY="<the Mixed Reality account primary or secondary key>"pipinstallazure-mixedreality-authentication

pythonsamples\client_sample.py
pythonsamples\client_sample_async.pyTroubleshootingThetroubleshootingsection for Azure Identity can be helpful when troubleshooting authentication issues.Next stepsMixed Reality client librariesComing soonContributingThis 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.If you'd like to contribute to this library, please read thecontributing guideto learn more about how to
build and test the code.Release History1.0.0b1 (2021-11-12)Features AddedInitial release with support for authenticating with Mixed Reality services.