Azure:尝试创建通知集线器名称时授权失败

时间:2017-05-14 18:01:52

标签: node.js azure

使用node.js SDK调用Azure通知中心api,我使用Azure Active目录注册了一个应用程序,我也创建了一个资源组,并将我的用户添加为所有者。但是在调用api时遇到这个错误:

AuthorizationFailed

  

客户端'333848ca-f996-XXXXXXXXXXXX',带有对象ID   '333848ca-f996-XXXXXXXXX'无权执行   动作'Microsoft.NotificationHubs / namespaces / write'超出范围   “/subscriptions/ef8e8e-XXXXXXXXXXXX/resourceGroups/notificationGroup/providers/Microsoft.NotificationHubs/namespaces/namespaceapp1

代码:

  MsRest.loginWithServicePrincipalSecret(
    My Subscription ID,
    My Application Secret Key,
    My Directory ID, // found under AD -> properties 
    (err, credentials) => {
      if (err) throw err;
      // eslint-disable-next-line 
      const client = new notificationHubsClient(credentials, AZURE_SUBSCRIPTION_ID);
      const createNamespaceParameters = {
        location: AZURE_LOCATION,
        tags: {
          tag1: 'value1',
          tag2: 'value2',
        },
      };
      client.namespaces.createOrUpdate(AZURE_RESOURCE_GROUP_NAME, req.body.name, createNamespaceParameters,
        (error, result) => {
          if (error) {
            res.send(error);
          }
          console.info(`Namespace created${JSON.stringify(result, null, ' ')}`);
          res.send('app created');
        });
    },
  );

1 个答案:

答案 0 :(得分:0)

根据错误消息,您需要将Azure Active Directory应用程序添加到 Contributor 角色,以便在Azure Notification Hub中创建命名空间。

enter image description here

您可以按照以下步骤进行操作:here

  1. 转到Azure portal并选择订阅
  2. enter image description here

    1. 选择要为其分配应用程序的特定订阅(资源组或资源)。
    2. enter image description here

      1. 选择访问控制(IAM)
      2. enter image description here

        1. 选择添加
        2. enter image description here

          1. 选择要分配给AAD应用程序的贡献者角色。
          2. enter image description here