Azure从Azure Java SDK检索VirtualMachines的PublicIPAddress

由于答案是Page Not Found,我试图使用以下代码检索公共IP的大小

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 PublicIpAddressList =PublicIpAddressListResponse.getPublicIpAddresses(); System.out.println(PublicIpAddressList.size()); 

使用Azure AD ServicePrincipal身份validation,它返回 – 0

使用带有“ https://management.azure.com/”API的证书身份validation,它返回 – 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地址? 或者如何validation它以获得IP值?

该问题是由使用不正确的身份validation引起的。

以下身份validation代码仅适用于Azure服务管理。

 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资源管理进行身份validation,文档“validationAzure资源管理请求”( https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx )说“您使用资源执行的所有任务必须使用Azure Active Directory对Azure资源管理器进行身份validation。“

因此,您需要使用您的subscription-id,tenant-id,client-id和client-secret修改身份validation配置代码,如下所示:

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

有关ServicePrincipal for Java的完整身份validation代码,请参阅https://github.com/Azure/azure-sdk-for-java/blob/master/azure-mgmt-samples/src/main/java/com/microsoft/ azure / samples / authentication / ServicePrincipalExample.java 。

对于线程( 使用azure java sdk在windows azure中检索订阅的网络列表 )回答的URL,它会移动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 。