Access Azure Media Services API with the Azure CLI

To use the Azure AD service principal authentication to connect to the Azure Media Services API, Your application needs to request an Azure AD token that has the following parameters:

  • Azure AD tenant endpoint
  • Media Services resource URI
  • Resource URI for REST Media Services
  • Azure AD application values: the client ID and client secret

For detailed explanation, see Accessing Media Services v3 APIs.

This article shows you how to use the Azure CLI to create an Azure AD application and service principal and get the values that are needed to access Azure Media Services resources.

Prerequisites

Create a Media Services account.

Make sure to remember the values that you used for the resource group name and Media Services account name.

CLI Shell

It is recommended to use Azure Cloud Shell to execute CLI commands. Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. It provides the flexibility of choosing the shell experience that best suits the way you work. Linux users can opt for a Bash experience, while Windows users can opt for PowerShell.

If you choose to install and use the CLI locally, this article requires that you are running the Azure CLI version 2.0 or later. Run az --version to find the version. If you need to install or upgrade, see Install the Azure CLI.

Login

To start using the CLI shell (in the cloud or locally), run az login to create a connection with Azure.

If the CLI can open your default browser, it will do so and load a sign-in page. Otherwise, you need to open a browser page and follow the instructions on the command line to enter an authorization code after navigating to https://aka.ms/devicelogin in your browser.

Specify location of files

Many Media Services CLI commands allow you to pass a parameter with a file name. If you are using Cloud Shell, you can upload your file to your clouddrive (using Bash or PowerShell).

Upload files

Whether you are using a local CLI or Cloud Shell, you need to specify the file path according to the OS or Cloud Shell (Bash or PowerShell) that you are using. Below are some examples:

Relative path to the file (all OS)

  • @"mytestfile.json"
  • @"../mytestfile.json"

Absolute file path on Linux/Mac and Windows OS

  • @ "/usr/home/mytestfile.json"
  • @"c:\tmp\user\mytestfile.json"

Use {file} if the command is asking for a path to the file. For example, az ams transform create -a amsaccount -g resourceGroup -n custom --preset .\customPreset.json.
Use @{file} if the command is going to load the specified file. For example, az ams account-filter create -a amsaccount -g resourceGroup -n filterName --tracks @tracks.json.

Access the Media Services API

To connect to Azure Media Services APIs, you use the Azure AD service principal authentication. The following command creates an Azure AD application and attaches a service principal to the account. You should use the returned values to configure your application.

Before running the script, you should replace the amsaccount and amsResourceGroup with the names you chose when creating these resources. amsaccount is the name of the Azure Media Services account where to attach the service principal.

The following command returns a json output:

az ams account sp create --account-name amsaccount --resource-group amsResourceGroup

This command produces a response similar to this:

{
  "AadClientId": "00000000-0000-0000-0000-000000000000",
  "AadEndpoint": "https://login.microsoftonline.com",
  "AadSecret": "00000000-0000-0000-0000-000000000000",
  "AadTenantId": "00000000-0000-0000-0000-000000000000",
  "AccountName": "amsaccount",
  "ArmAadAudience": "https://management.core.windows.net/",
  "ArmEndpoint": "https://management.azure.com/",
  "Region": "West US 2",
  "ResourceGroup": "amsResourceGroup",
  "SubscriptionId": "00000000-0000-0000-0000-000000000000"
}

If you would like to get an xml in the response, use the following command:

az ams account sp create --account-name amsaccount --resource-group amsResourceGroup --xml

See also

Next steps

The Streaming Endpoint from which you want to stream content has to be in the Running state. The following CLI command starts your default Streaming Endpoint:

az ams streaming-endpoint start -n default -a <amsaccount> -g <amsResourceGroup>