无法从Azure java SDK列出图像发布者

时间:2016-01-26 08:12:13

标签: java azure sdk

我正在使用Azure ARM API,并尝试通过Azure Java SDK按位置列出所有发布者,方法是执行以下代码:

import com.microsoft.azure.management.compute.ComputeManagementClient;
import com.microsoft.azure.management.compute.ComputeManagementService;
import com.microsoft.azure.management.compute.models.VirtualMachineImageListPublishersParameters;
import com.microsoft.azure.management.compute.models.VirtualMachineImageResourceList;

@Test
public void testListPublishers() {
    ComputeManagementClient client = ComputeManagementService.create(createConfiguration());
    VirtualMachineImageListPublishersParameters params = new VirtualMachineImageListPublishersParameters();
    params.setLocation("westus");
    VirtualMachineImageResourceList response = client.getVirtualMachineImagesOperations().listPublishers(params);
    ArrayList<VirtualMachineImageResource> resources = response.getResources();
    System.out.println("Found publishers: " + resources.size());
}

这导致以下请求:

GET /subscriptions/{some-subscription}/providers/Microsoft.Compute/locations/westus/publishers?api-version=2015-06-15

但是,无论我在发布商参数中放置的位置如何,我总是得到空列表。 我能够使用相同的客户端列出其他资源,因此在创建客户端时不会出现问题。

你对我可能做错了什么有什么建议吗?也许有一个我没有的许可?

谢谢!

1 个答案:

答案 0 :(得分:0)

根据我的经验,该问题是由Azure AD上注册的应用程序没有Reader角色引起的。我重现了这个问题,并通过为AzureAD应用程序分配一个Reader角色来解决它。

分配读者角色有两种方式。

  1. 将Azure-CLI与arm模式配合使用,并命令azure ad role assignment create --objectId <objectId of the aad app> -o Reader -c /subscriptions/<subscriptionId>/
  2.   

    如果您不知道AzureAD应用程序的objectId,可以命令azure ad sp show --search <the aad app name>进行查看。如果Azure AD没有Service Principal (SP),则可以命令azure ad sp create <clientId>创建它。

    1. 当Azure新门户网站上显示的应用程序时,通过All settings -> RESOURCE MANAGEMENT -> Users添加角色和用户,请参阅下面的图片。
    2. 选择角色Readerenter image description here

      通过搜索名称添加用户: enter image description here

      将读者角色分配给aad应用后,您可以根据需要列出图片发布者。