Azure Java SDK: ServiceException: ForbiddenError:

前端 未结 1 641
萌比男神i
萌比男神i 2021-01-27 07:49

Tried the basic location retriever code (shown below)

String uri = \"https://management.core.windows.net/\";
        String subscriptionId = \"XXXXXXXX-5fad-XXX         


        
1条回答
  •  野性不改
    2021-01-27 08:14

    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.

    
       com.microsoft.azure
       azure-mgmt-compute
       0.8.3
    
    

    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.

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

    The right maven pom.xml for dependencies as below:

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

    And the code as below

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

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

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

    My code:

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

    0 讨论(0)
提交回复
热议问题