【问题标题】:'Insufficient privileges to complete the operation' when trying to create group via Microsoft Graph尝试通过 Microsoft Graph 创建组时“权限不足,无法完成操作”
【发布时间】:2023-03-19 13:39:01
【问题描述】:

期望的行为

通过 Microsoft Graph 创建一个 group,在具有 delegated API 权限的 single tenant application 中使用 JavaScript SDK 和 MSAL。

实际行为

{
  "statusCode": 403,
  "code": "Authorization_RequestDenied",
  "message": "Insufficient privileges to complete the operation.",
  "requestId": "6e865d96-ef8b-408e-a905-5393b4adcfdc",
  "date": "2020-05-16T20:20:15.000Z",
  "body": "{\"code\":\"Authorization_RequestDenied\",\"message\":\"Insufficient privileges to complete the operation.\",\"innerError\":{\"request-id\":\"6e865d96-ef8b-408e-a905-5393b4adcfdc\",\"date\":\"2020-05-17T06:20:15\"}}"
}

我的尝试

为了创建代码,我使用了以下 API 参考说明:

登录/注销和所有其他 API 请求都正常工作。

订阅

Microsoft 365 Business Standard Trial

API 权限

Directory.AccessAsUser.All +++
Directory.ReadWrite.All ***
Group.ReadWrite.All  
GroupMember.ReadWrite.All ***
openid
profile
Sites.Read.All
Tasks.ReadWrite
User.Read

*** 我在阅读this answer 后添加了这些,但仍然出现相同的错误。
+++ 我在下面的回答中提出建议后添加了这个。

我已单击 Azure 应用注册区域中的“授予管理员同意”按钮。

index.html

<!-- msal -->
<!-- from:  https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-core -->
<script type="text/javascript" src="https://alcdn.msauth.net/lib/1.3.0/js/msal.js" integrity="****" crossorigin="anonymous"></script>

<!-- javascript sdk -->
<!-- from:  https://github.com/microsoftgraph/msgraph-sdk-javascript -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@microsoft/microsoft-graph-client/lib/graph-js-sdk.js"></script>

config.js

// msal options
const msalConfig = {
    auth: {
        clientId: "client-id-here",
        redirectUri: "http://localhost:8080",
        authority: "https://login.microsoftonline.com/tenant-id-here"
    },
  cache: {
    cacheLocation: "sessionStorage",
    storeAuthStateInCookie: false,
    forceRefresh: false
  }
};

// define application permissions   
const scopes = ['directory.accessasuser.all', 'directory.readwrite.all', 'group.readwrite.all', 'groupmember.readwrite.all', 'openid', 'profile', 'sites.read.all', 'user.read', 'tasks.readwrite' ]; 

auth.js

const msalApplication = new Msal.UserAgentApplication(msalConfig);

const loginRequest = {
    scopes: scopes
}

async function sign_in() {
    try {
        await msalApplication.loginPopup(loginRequest);
        if (msalApplication.getAccount()) {
            console.log("sign in success");
        }
    } catch (error) {
        console.log("sign in error");
        console.log(error);
    }
}

function sign_out() {
    msalApplication.logout();
}

graph.js

var path = "/groups"; 
var group = {
  description: "Here is a description.",
  displayName: "test_test_test",
  groupTypes: [
    "Unified",
    "DynamicMembership"
  ],
  mailEnabled: true,
  mailNickname: "test_test_test",
  securityEnabled: false, // initially i had this as true, doesn't seem to make a difference either way  
  visibility: "Hiddenmembership",
  "owners@odata.bind": [
    "https://graph.microsoft.com/v1.0/users/my-user-id-here"
  ],
    "members@odata.bind": [
        "https://graph.microsoft.com/v1.0/users/my-user-id-here"
  ]
};

var response = await client.api(path)
                            .post(group);

相关阅读:

Compare groups

Learn about Microsoft 365 Groups

Overview of Microsoft 365 Groups for administrators

Working with groups in Microsoft Graph

Create, edit, or delete a security group in the Microsoft 365 admin center

Creating teams and managing members using Microsoft Graph

Overview of Office 365 groups in Microsoft Graph

关于可能导致错误的其他想法...

仅统一组支持可见性;安全组不支持它。

Source

可能securityEnabled: truevisibility:Hiddenmembership的组合不兼容?

我尝试将其更改为 securityEnabled: false 并收到相同的消息。

令牌有问题吗?

我从 Chrome 开发工具控制台复制了 Authorisation: Bearer 值并粘贴到 https://jwt.io 并且令牌中提供了两个额外的范围 - 但所有其他范围都存在:

【问题讨论】:

  • 它是否要求您的 Azure 广告租户管理员批准?在管理员批准之前,我无法使用 user.read.all 权限。此外,这适用于没有用户的守护程序应用程序。
  • 我已单击 Azure 应用注册区域中的“授予管理员同意”按钮(将此信息添加到帖子中)。

标签: microsoft-graph-api microsoft-graph-sdks msal.js


【解决方案1】:

从您的图表调用中,我看到您尝试创建动态组并为该组分配成员,动态组不支持添加成员,这就是您删除组类型下"dynamicMemberShip"的原因可以修复这个问题,下面是通过graph API调用创建动态组的例子:

{
    "description": "test1",
    "displayName": "test1",
    "groupTypes": [
        "Unified",
        "DynamicMembership"
    ],
    "mailEnabled": true,
    "membershipRule": "user.displayname -contains A",
    "membershipRuleProcessingState":"On",
    "mailNickname": "library",
    "securityEnabled": false,
}

【讨论】:

  • 哦,根据我在其他地方读到的内容,我对动态组的理解有所不同,但这似乎是一个很好的解释:docs.microsoft.com/en-us/microsoftteams/dynamic-memberships 所以你的答案中解释的原因是为什么我收到特定的“权限不足”错误?
  • 是的,这个答案可以解释为什么您会遇到特定的“权限不足”错误以及通过图形 API 调用创建动态组的正确格式
【解决方案2】:

我可以看到您没有在您的权限集中授予 Group.Create 权限。 授予此权限以创建群组。

对于应用权限类型,这些所有权限都是必需的:

  • 组.创建
  • Group.ReadWrite.All
  • Directory.ReadWrite.All

对于委派用户权限类型

  • Group.ReadWrite.All
  • Directory.ReadWrite.All
  • Directory.AccessAsUser.All

【讨论】:

  • 权限是delegated,而不是application,我已将此详细信息添加到问题中,谢谢。我在Microsoft Graph > Delegated permissions > Group 区域只看到两个权限:Group.Read.AllGroup.ReadWrite.All,没有Group.Create。我尝试在 app 和 azure 中添加 Directory.AccessAsUser.All 并得到同样的错误。
  • 您的意思是我必须添加Application Delegated 权限吗?我以为我只需要为我的场景添加Delegated 权限。
  • 它应该只适用于委托权限,无需添加应用程序权限,您是否在 Azure 门户上指定了权限?
  • 您是否通过客户端密码获取身份验证令牌? @user1063287
  • 是的,在 azure 和应用代码中具有指定的权限。所有的身份验证都由 MSAL 处理,javascript SDK、登录/注销和其他请求工作正常。
【解决方案3】:

我可以通过更改graph.js 中的以下内容来创建Group

groupTypes: ["Unified","DynamicMembership"]  

groupTypes: ["Unified"]`   

我不知道为什么DynamicMembership 会导致错误。

这是Group > groupTypes 的有效属性值,列在下面的链接中,没有任何警告:

https://docs.microsoft.com/en-us/graph/api/resources/group?view=graph-rest-1.0#properties

供参考,上面的代码创建:

  • SharePoint“团队网站”
  • Outlook 电子邮件组

不会自动创建对应的:

  • 团队
  • 计划

您可以在以下位置看到新组:

https://admin.microsoft.com/AdminPortal/Home#/groups

https://outlook.office365.com/people

https://portal.azure.com/#blade/Microsoft_AAD_IAM/GroupsManagementMenuBlade/AllGroups

您可以在以下位置看到组“团队站点”:

https://your_account-admin.sharepoint.com/_layouts/15/online/AdminHome.aspx#/siteManagement

【讨论】:

    最近更新 更多