Connect to Media Services v3 API - Python
Looking for Media Services v2 documentation?
Having trouble? See the Troubleshooting guide for solutions to issues with using Media Services.
Code samples can be found on the Samples page.
This article shows you how to connect to the Azure Media Services v3 Python SDK using the service principal sign in method.
Prerequisites
- Download Python from python.org
- Make sure to set the
PATHenvironment variable - Create a Media Services account. Be sure to remember the resource group name and the Media Services account name.
- Follow the steps in the Access APIs topic, selecting the Service principal authentication method. Record the subscription ID, application client ID, the authentication key, and the tenant ID that you need in the later steps.
Important
Review naming conventions.
Install the modules
To work with Azure Media Services using Python, you need to install these modules.
The
azure-identitymodule, which includes Azure modules for Active Directory.The
azure-mgmt-mediamodule, which includes the Media Services entities.Make sure to get the latest version of the Media Services SDK for Python.
Open a command-line tool and use the following commands to install the modules.
pip3 install azure-identity
pip3 install azure-mgmt-media
Connect to the Python client
Create a file with a
.pyextensionOpen the file in your favorite editor
Add the the following code to the file. The code imports the required modules and creates the Active Directory credentials object you need to connect to Media Services.
Set the variables' values to the values you got from Access APIs. Update the
ACCOUNT_NAMEandRESOURCE_GROUP_NAMEvariables to the Media Services account name and Resource Group names used when creating those resources.from azure.identity import ClientSecretCredential from azure.mgmt.media import AzureMediaServices # Tenant ID for your Azure Subscription TENANT_ID = "(update-this-value)" # Your Application Client ID of your Service Principal CLIENT_ID = "(update-this-value)" # Your Service Principal secret key CLIENT_SECRET = "(update-this-value)" # Your Azure Subscription ID SUBSCRIPTION_ID = "(update-this-value)" # Your Resource Group name RESOURCE_GROUP_NAME = "(update-this-value)" # Your Azure Media Service account name ACCOUNT_NAME = "(update-this-value)" credentials = ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET) # The Azure Media Services Client client = AzureMediaServices(credentials, SUBSCRIPTION_ID) # Now that you are authenticated, you can manipulate the entities. # For example, list assets in your Media Services account assets = client.assets.list(RESOURCE_GROUP_NAME, ACCOUNT_NAME) for i, r in enumerate(assets): print(r)Run the file
Additional samples
Additional samples are available in GitHub in the Azure Media Services v3 Python Samples repo.