Azure的检索VirtualMachines的PublicIPAddress从Azure的SDK的Java [英] Azure retrieving the PublicIPAddress of VirtualMachines from Azure Java SDK

查看:269
本文介绍了Azure的检索VirtualMachines的PublicIPAddress从Azure的SDK的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着<一个href=\"http://stackoverflow.com/questions/27059680/retrieve-list-of-networks-for-a-subscription-in-windows-azure-with-azure-java-sd\">answer在找不到网页,我试图检索与以下code的公网IP​​的尺寸

As the answer was Page Not Found, I was trying to retrieve size of Public IPs with the following code

Configuration config = ManagementConfiguration.configure(
          new URI(uri), 
          subscriptionId,
          keyStoreLocation, // the file path to the JKS
          keyStorePassword, // the password for the JKS
          KeyStoreType.jks // flags that I'm using a JKS keystore
        );

NetworkResourceProviderClient  networkResourceProviderClient = NetworkResourceProviderService.create(config);
           PublicIpAddressListResponse PublicIpAddressListResponse =networkResourceProviderClient.getPublicIpAddressesOperations().listAll();
           ArrayList<PublicIpAddress> PublicIpAddressList =PublicIpAddressListResponse.getPublicIpAddresses();
           System.out.println(PublicIpAddressList.size());

使用Azure的AD ServicePrincipal认证,它返回 - 0

using Azure AD ServicePrincipal authentication, It returns - 0

https://management.azure.com/ API,它返回 - AuthenticationFailed:

using certificate authentication with the "https://management.azure.com/" API, It returns - AuthenticationFailed:

Exception in thread "main" com.microsoft.windowsazure.exception.ServiceException: AuthenticationFailed: Authentication failed. The 'Authorization' header is not present or provided in an invalid format.
    at com.microsoft.windowsazure.exception.ServiceException.createFromJson(ServiceException.java:290)
    at com.microsoft.azure.management.network.PublicIpAddressOperationsImpl.listAll(PublicIpAddressOperationsImpl.java:1443)
    at com.microsoft.azure.auth.Program.main(Program.java:50)

任何想法如何获取虚拟机的所有公共IP地址?或如何对其进行身份验证,以获得知识产权的价值?

any Idea how to retrieve all the Virtual Machine's Public IP address? or how to Authenticate it to get the IP value?

推荐答案

这个问题是通过使用不正确的验证而引起的。

The issue was caused by using the incorrect authentication.

下面的验证code只适用于Azure的服务管理。

The authentication code below just works for Azure Service Management.

Configuration config = ManagementConfiguration.configure(
          new URI("https://management.core.windows.net), 
          subscriptionId,
          keyStoreLocation, // the file path to the JKS
          keyStorePassword, // the password for the JKS
          KeyStoreType.jks // flags that I'm using a JKS keystore
        );

对于鉴定Azure的资源管理,商务部的认证的Azure资源管理要求(的 https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx )说:所有的您在使用Azure的资源管理器的资源完成的任务必须与Azure的活动进行身份验证目录。

Authenticating for Azure Resource Management, the doc "Authenticating Azure Resource Management request"( https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx) says "All of the tasks that you do on resources using the Azure Resource Manager must be authenticated with Azure Active Directory. ".

所以,你需要修改身份验证配置code用订阅的标识,租户ID,客户端ID和客户端秘密如下:

So you need to modify the authenticate configuration code with your subscription-id, tenant-id, client-id and client-secret as follow:

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.windows.net/" + "<your tenant id>",
                    false, service);
            // TODO: add your client id and client secret
            ClientCredential cred = new ClientCredential("<your client id>",
                    "<your client secret>");
            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;
    }


Configuration config = ManagementConfiguration.configure(
          null,
          new URI("https://management.core.windows.net), 
          "<your-subscription-id>",
          getAccessTokenFromServicePrincipalCredentials()
.getAccessToken()
        );

有关ServicePrincipal完整的身份验证code对Java,请参照<一个href=\"https://github.com/Azure/azure-sdk-for-java/blob/master/azure-mgmt-samples/src/main/java/com/microsoft/azure/samples/authentication/ServicePrincipalExample.java\" rel=\"nofollow\">https://github.com/Azure/azure-sdk-for-java/blob/master/azure-mgmt-samples/src/main/java/com/microsoft/azure/samples/authentication/ServicePrincipalExample.java.

有关线程(<一个href=\"http://stackoverflow.com/questions/27059680/retrieve-list-of-networks-for-a-subscription-in-windows-azure-with-azure-java-sd\">Retrieve网络在Windows Azure天青Java SDK的)答案的网址订阅列表,它移动<一个href=\"https://github.com/Azure/azure-sdk-for-java/blob/master/service-management/azure-svc-mgmt-network/src/main/java/com/microsoft/windowsazure/management/network/NetworkOperations.java\" rel=\"nofollow\">https://github.com/Azure/azure-sdk-for-java/blob/master/service-management/azure-svc-mgmt-network/src/main/java/com/microsoft/windowsazure/management/network/NetworkOperations.java.

For the thread(Retrieve List of networks for a subscription in windows azure with azure java sdk) answer's url, it moves https://github.com/Azure/azure-sdk-for-java/blob/master/service-management/azure-svc-mgmt-network/src/main/java/com/microsoft/windowsazure/management/network/NetworkOperations.java.

这篇关于Azure的检索VirtualMachines的PublicIPAddress从Azure的SDK的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆