2
votes

I would like to list available IP VM's in the new Azure portal using Java SDK.

Couple of years back in the good old classic portal, I had followed the usual management certificate procedure to access vm's,create vm's and work with Azure Endpoints.

Fast fwd now I see that they have used a new portal and new mechanisms to interact with Java SDK. I read somewhere in the above link that with the old way with certificates, I can manage only the class portal resources.

I'm trying to code a simple program which authenticates and lists the vm's of the new portal as a start. Seems like they have complicated it a lot.

I followed the below link to "Create service principal with password"

https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/

Then I went to this link

https://azure.microsoft.com/en-us/documentation/samples/resources-java-manage-resource-group/

which asked me go the "See how to create an Auth file" link in above page

(mine is not a webapp and when I try to create the AD as a native client application, it is not allowing me to save keys in configure tab, so I had to create a web app)

After doing all this, I got stuck with this below error

    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for         further details.
    'authority' Uri should have at least one segment in the path (i.e.https://<host>/<path>/...)
   java.lang.IllegalArgumentException: 'authority' Uri should have at least one segment in the path (i.e. https://<host>/<path>/...)
    at com.microsoft.aad.adal4j.AuthenticationAuthority.detectAuthorityType(AuthenticationAuthority.java:190)
    at com.microsoft.aad.adal4j.AuthenticationAuthority.<init>(AuthenticationAuthority.java:73)

When I checked it says that the error is because I don't have a valid client application id in your Azure Active Directory.

Is there any simple way to authenticate and start using the API's?

1

1 Answers

1
votes

@Vikram, I suggest that you can try to refer to the article to create an application on AAD.

Then you can follow the code below to get the access token for authentication.

// The parameters include clientId, clientSecret, tenantId, subscriptionId and resourceGroupName.
private static final String clientId = "<client-id>";
private static final String clientSecret = "<key>";
private static final String tenantId = "<tenant-id>";
private static final String subscriptionId = "<subscription-id>";

// The function for getting the access token via Class AuthenticationResult
private static AuthenticationResult getAccessTokenFromServicePrincipalCredentials()
        throws ServiceUnavailableException, MalformedURLException, ExecutionException, InterruptedException {
    AuthenticationContext context;
    AuthenticationResult result = null;
    ExecutorService service = null;
    try {
        service = Executors.newFixedThreadPool(1);
        // TODO: add your tenant id
        context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false, service);
        // TODO: add your client id and client secret
        ClientCredential cred = new ClientCredential(clientId, clientSecret);
        Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", cred, null);
        result = future.get();
    } finally {
        service.shutdown();
    }

    if (result == null) {
        throw new ServiceUnavailableException("authentication result was null");
    }
    return result;
}

String accessToken = getAccessTokenFromServicePrincipalCredentials().getAccessToken();

If you want to list the VMs on new portal, you can try to use the REST API List the resources in a subscription to get all resources and filter the VMs via the resource type Microsoft.Compute/virtualMachines.

Hope it helps.