Azure Java SDK: ServiceException: ForbiddenError:
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<Location> 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 依赖项时,我重现了相同的异常。
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-mgmt-compute</artifactId>
<version>0.8.3</version>
</dependency>
提供VM重启功能的库需要两个参数:resource group name
和vm name
。但是库 azure-mgmt-compute
的 API 用于 Azure 资源管理。
要重启虚拟机,如果您使用了 JKS 证书,则需要使用库 azure-svc-mgmt-compute
的 API 用于 Azure 服务管理。 Class VirtualMachineOperations
提供同名函数 restart
需要三个参数:service name
、deployment name
和 vm name
。您可以从 Azure 门户上的云服务仪表板找到这些名称。在您的问题代码中,vm name
应该是 "sqlvm".
正确的 maven pom.xml 依赖如下:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt</artifactId>
<version>0.8.3</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-compute</artifactId>
<version>0.8.3</version>
</dependency>
代码如下
virtualMachinesOperations.restart("<service name: sqlvm>", "<deployment name: sqlvm>", "<vm name: sqlvm>");
通过在路径 JAVA_HOME/bin:
中使用 Java Keytool 生成密钥对的以下步骤
keytool -genkeypair -alias keyfile -keyalg RSA -keystore <KeyStore.jks>
-keysize 2048 -storepass "<password>"
keytool -v -export -file <KeyStore.cer> -keystore KeyStore.jks -alias keyfile
我的代码:
String uri = "https://management.core.windows.net/";
String subscriptionId = "<subscription_id>";
String keyStoreLocation = "KeyStore.jks";
String keyStorePassword = "<password>";
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");
尝试了基本位置检索器代码(如下所示)
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<Location> 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 依赖项时,我重现了相同的异常。
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-mgmt-compute</artifactId>
<version>0.8.3</version>
</dependency>
提供VM重启功能的库需要两个参数:resource group name
和vm name
。但是库 azure-mgmt-compute
的 API 用于 Azure 资源管理。
要重启虚拟机,如果您使用了 JKS 证书,则需要使用库 azure-svc-mgmt-compute
的 API 用于 Azure 服务管理。 Class VirtualMachineOperations
提供同名函数 restart
需要三个参数:service name
、deployment name
和 vm name
。您可以从 Azure 门户上的云服务仪表板找到这些名称。在您的问题代码中,vm name
应该是 "sqlvm".
正确的 maven pom.xml 依赖如下:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt</artifactId>
<version>0.8.3</version>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-svc-mgmt-compute</artifactId>
<version>0.8.3</version>
</dependency>
代码如下
virtualMachinesOperations.restart("<service name: sqlvm>", "<deployment name: sqlvm>", "<vm name: sqlvm>");
通过在路径 JAVA_HOME/bin:
中使用 Java Keytool 生成密钥对的以下步骤keytool -genkeypair -alias keyfile -keyalg RSA -keystore <KeyStore.jks>
-keysize 2048 -storepass "<password>"
keytool -v -export -file <KeyStore.cer> -keystore KeyStore.jks -alias keyfile
我的代码:
String uri = "https://management.core.windows.net/";
String subscriptionId = "<subscription_id>";
String keyStoreLocation = "KeyStore.jks";
String keyStorePassword = "<password>";
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");