2015-10-14 74 views
0

由于answer检索VirtualMachines的PublicIPAddress是找不到网页,我试图用下面的代码天青从Azure中的Java SDK

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认证,它返回检索的公网IP的大小 - 0

使用证书认证与https://management.azure.com/ API,它返回 - 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地址?或者如何对其进行身份验证以获取IP值?

回答

1

该问题是由于使用不正确的身份验证造成的。

下面的身份验证代码仅适用于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的资源管理,商务部的“认证Azure的资源管理要求”(https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx)说:“所有的您在使用Azure的资源管理器的资源完成的任务必须与Azure的活动目录进行身份验证。” 。

所以,你需要修改与您订阅的ID,租户ID,客户端ID和客户端秘密的身份验证配置代码如下:

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用于Java的完整身份验证码,请请参阅https://github.com/Azure/azure-sdk-for-java/blob/master/azure-mgmt-samples/src/main/java/com/microsoft/azure/samples/authentication/ServicePrincipalExample.java

对于线程(Retrieve List of networks for a subscription in windows azure with azure java sdk)答案的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