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

问题描述

试过基本位置猎犬code(如下图所示)

Tried the basic location retriever code (shown below)

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并尝试重新启动虚拟机

and It works fine. But when I try to create the ComputeManagementClient and try to restart a VM

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

我得到证书错误。

I'm getting the certificate error.

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密钥的.CER并上传到Azure的,没有问题。

PS: I created a the .cer from Java Keystore and uploaded into Azure with no issues.

任何线索发生了什么?

推荐答案

这个问题是通过使用不正确的Azure SDK的Java库造成的。
当我使用Maven依赖下面的文件pom.xml中,我复制相同的异常。

The issue is caused by using the incorrect Azure Java SDK libraries.When I used the maven dependencies in the file pom.xml below, I reproduced the same exception.

<dependency>
   <groupId>com.microsoft.azure</groupId>
   <artifactId>azure-mgmt-compute</artifactId>
   <version>0.8.3</version>
</dependency>

该库提供虚拟机重启功能需要两个参数:资源组的名称虚拟机名称。但库的API Azure的MGMT-计算用于Azure的资源管理。

The library supply the VM restart function need two arguments: resource group name and vm name. But the API of library azure-mgmt-compute is used for Azure Resource Management.

要重新启动虚拟机,你需要使用,如果你使用JKS证书库蔚蓝-SVC-MGMT-计算对Azure的服务管理的API。类 VirtualMachineOperations 提供的同名函数重新启动需要三个参数:服务名称部署名称虚拟机名称。你可以找到在Azure门户云服务的仪表板这些名称。在您的问题code时,虚拟机名称应该是sqlvm。

To restart VM, you need to use the API of library azure-svc-mgmt-compute for Azure Service Management if you used JKS certificates. The Class VirtualMachineOperations supply the same name function restart need three arguments: service name, deployment name and vm name. You can find these names from Cloud Service dashboard on Azure Portal. In your issue code, the vm name should be "sqlvm".

右边的Maven的pom.xml依赖项,如下:

The right maven pom.xml for dependencies as below:

<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>

而code如下

virtualMachinesOperations.restart("<service name: sqlvm>", "<deployment name: sqlvm>", "<vm name: sqlvm>");


由路径JAVA_HOME / bin中使用Java的keytool低于genkeypair的步骤:


The steps below for genkeypair by using Java Keytool in the path JAVA_HOME/bin:

keytool -genkeypair -alias keyfile -keyalg RSA -keystore <KeyStore.jks>
-keysize 2048 -storepass "<password>"

keytool -v -export -file <KeyStore.cer> -keystore KeyStore.jks -alias keyfile

我的code:

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");

这篇关于Azure的Java SDK的:ServiceException:ForbiddenError:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:33