Connect to Media Services v3 API - Java
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_HOMEandPATHenvironment 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
In Visual Studio Code, open the folder where your project is
Find and open the
pom.xmlAdd the needed dependencies.
See
pom.xmlin the Video encoding sample.
Connect to the Java client
Open the
App.javafile undersrc\main\java\com\azure\amsand make sure your package is included at the top:package com.azure.ams;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;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()); }Run the app.