Connect to Media Services v3 API - Java

media services logo v3


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 Java SDK using the service principal sign in method.

In this article, the Visual Studio Code is used to develop the sample app.

Prerequisites

  • Follow Writing Java with Visual Studio Code to install:

    • JDK
    • Apache Maven
    • Java Extension Pack
  • Make sure to set JAVA_HOME and PATH environment variables.

  • 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. Record the subscription ID, application ID (client ID), the authentication key (secret), and the tenant ID that you need in a later step.

Also review:

Important

Review naming conventions.

Create a Maven project

Open a command-line tool and cd to a directory where you want to create the project.

mvn archetype:generate -DgroupId=com.azure.ams -DartifactId=testAzureApp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

When you run the command, the pom.xml, App.java, and other files are created.

Add dependencies

  1. In Visual Studio Code, open the folder where your project is

  2. Find and open the pom.xml

  3. Add the needed dependencies.

    See pom.xml in the Video encoding sample.

Connect to the Java client

  1. Open the App.java file under src\main\java\com\azure\ams and make sure your package is included at the top:

    package com.azure.ams;
    
  2. Under the package statement, add these import statements:

    import com.azure.core.management.AzureEnvironment;
    import com.azure.core.management.profile.AzureProfile;
    import com.azure.identity.ClientSecretCredential;
    import com.azure.identity.ClientSecretCredentialBuilder;
    import com.azure.resourcemanager.mediaservices.MediaServicesManager;
    
  3. To create the Active Directory credentials that you need to make requests, add following code to the main method of the App class and set the values that you got from Access APIs:

    try {
         AzureProfile azureProfile = new AzureProfile("<YOUR_TENANT_ID>", "<YOUR_SUBSCRIPTION_ID>", AzureEnvironment.AZURE);
         ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
             .clientId("<YOUR_CLIENT_ID>")
             .clientSecret("<YOUR_CLIENT_SECRET>")
             .tenantId("<YOUR_TENANT_ID>")
             // authority host is optional
             .authorityHost("<AZURE_AUTHORITY_HOST>")
             .build();
         MediaServicesManager mediaServicesManager = MediaServicesManager.authenticate(clientSecretCredential, azureProfile);
         System.out.println("Hello Azure");
    }
    catch (Exception e) {
       System.out.println("Exception encountered.");
       System.out.println(e.toString());
    }
    
  4. Run the app.