Azure Java SDK:ServiceException:ForbiddenError:

尝试了基本位置检索器代码(如下所示)

String uri = "https://management.core.windows.net/"; String subscriptionId = "XXXXXXXX-5fad-XXXXXX-9dfa-XXXXXX"; String keyStoreLocation = "D:\\test.jks"; String keyStorePassword = "123456"; 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 ); ManagementClient client = ManagementService.create(config); // get the list of regions LocationsListResponse response = client.getLocationsOperations().list(); ArrayList locations = response.getLocations(); // write them out for( int i=0; i<locations.size(); i++){ System.out.println(locations.get(i).getDisplayName()); } 

它工作正常。 但是当我尝试创建ComputeManagementClient并尝试重新启动VM时

 ComputeManagementClient computeManagementClient = ComputeManagementService.create(config); VirtualMachineOperations virtualMachinesOperations= computeManagementClient.getVirtualMachinesOperations(); virtualMachinesOperations.restart("SQLVM", "sqlvm.cloudapp.net"); 

我收到证书错误。

 Exception in thread "main" java.util.concurrent.ExecutionException: com.microsoft.windowsazure.exception.ServiceException: ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription. at java.util.concurrent.FutureTask.report(FutureTask.java:122) at java.util.concurrent.FutureTask.get(FutureTask.java:188) at com.microsoft.azure.management.compute.VirtualMachineOperationsImpl.restart(VirtualMachineOperationsImpl.java:9973) at com.microsoft.azure.compute.RestartVMExample.main(RestartVMExample.java:84) 

PS:我从Java Keystore创建了一个.cer并上传到Azure,没有任何问题。

有什么线索发生了什么?

该问题是由使用不正确的Azure Java SDK库引起的。 当我在下面的文件pom.xml中使用maven依赖项时,我重现了同样的exception。

  com.microsoft.azure azure-mgmt-compute 0.8.3  

提供VM重启function的库需要两个参数: resource group namevm name 。 但是, azure-mgmt-compute库的API用于Azure资源管理。

要重新启动VM,如果使用JKS证书,则需要使用库azure-svc-mgmt-compute的API进行Azure服务管理。 类VirtualMachineOperations提供相同名称的函数restart需要三个参数: service namedeployment namevm name 。 您可以从Azure门户上的Cloud Service仪表板中找到这些名称。 在您的问题代码中, vm name应为“sqlvm”。

适用于依赖项的正确maven pom.xml如下:

  com.microsoft.azure azure-svc-mgmt 0.8.3   com.microsoft.azure azure-svc-mgmt-compute 0.8.3  

代码如下

 virtualMachinesOperations.restart("", "", ""); 

通过在路径JAVA_HOME / bin中使用Java Keytool来获取genkeypair的以下步骤:

 keytool -genkeypair -alias keyfile -keyalg RSA -keystore  -keysize 2048 -storepass "" keytool -v -export -file  -keystore KeyStore.jks -alias keyfile 

我的代码:

 String uri = "https://management.core.windows.net/"; String subscriptionId = ""; String keyStoreLocation = "KeyStore.jks"; String keyStorePassword = ""; 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 ); ComputeManagementClient computeManagementClient = ComputeManagementService.create(config); VirtualMachineOperations virtualMachinesOperations = computeManagementClient.getVirtualMachinesOperations(); virtualMachinesOperations.restart("petercore", "petercore", "petercore");