【问题标题】:MS graph API Oauth2PermissionGrants cannot grant rolesMS 图形 API Oauth2PermissionGrants 无法授予角色
【发布时间】:2020-04-23 19:55:19
【问题描述】:

使用下面的代码,我可以第一次批准给予管理员同意。 但是当我第二次调用代码时。代码返回错误。

示例:

我可以在第一次请求时授予User.Read.All 的访问权限。 但是,当我想为第二个角色AccessReview.Read.All 授予管理员同意时,请求会给出以下错误

代码

 IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create("Client ID")
                .WithClientSecret("Client Secret")
                .WithTenantId("Tenant ID")
                .Build();



            string scopes = "https://graph.microsoft.com/.default";
            ClientCredentialProvider authProvider = new ClientCredentialProvider(app, scopes);
            Beta.GraphServiceClient graphClient = new Beta.GraphServiceClient(authProvider);

            Beta.OAuth2PermissionGrant test = new Beta.OAuth2PermissionGrant { ClientId = model.clientId, ConsentType = model.consentType, ExpiryTime = model.expiryTime, ResourceId = model.resourceId, Scope = model.scope };
            var response = await graphClient.Oauth2PermissionGrants
                .Request()
                .AddAsync(test);

            return response.ToString();

错误

Status Code: Conflict
Microsoft.Graph.ServiceException: Code: Request_MultipleObjectsWithSameKeyValue
Message: Permission entry already exists.
Inner error:
    AdditionalData:
    request-id: b9e44bc2-7588-4390-a3ca-9abdc213d930
    date: 2020-04-23T19:43:03
ClientRequestId: b9e44bc2-7588-4390-a3ca-9abdc213d930

【问题讨论】:

    标签: c# azure microsoft-graph-api microsoft-graph-sdks


    【解决方案1】:

    您不能两次添加相同的权限授予,就像错误消息中所说的,permission entry already exists。如果要更新权限授予,可以使用update方法:

    var oAuth2PermissionGrant = new OAuth2PermissionGrant
    {
        Scope = "scope-value"
    };
    
    await graphClient.OAuth2Permissiongrants["{id}"]
        .Request()
        .UpdateAsync(oAuth2PermissionGrant);
    

    参考:

    https://docs.microsoft.com/en-us/graph/api/oauth2permissiongrant-update?view=graph-rest-beta&tabs=http

    【讨论】:

    • 我没有两次添加相同的权限。我正在尝试同意新的许可。它给出了那个错误
    • @DixonJosephDalmeida 您是如何同意新权限的?
    • @DixonJosephDalmeida 如果您调用您提供的代码,您将再次添加授权。
    • 所以我同意 User.Read 和 User.Read.All 的管理员。我认为 Graph API 会为每个管理员同意创建新对象。但它只为每个同意类型创建一个对象。所以我需要像你上面所说的那样修补
    • 我已经在上面添加了我的答案。
    【解决方案2】:

    我发现了问题。 Graph API 根据同意类型为 Oauth Grant 权限创建单个对象。 因此,对于第一个请求,调用 post 请求以授予访问权限。 但是对于第二个请求,使用 patch 命令更新对象

    var oAuth2PermissionGrant = new OAuth2PermissionGrant
    {
        Scope = "scope-value"
    };
    
    await graphClient.OAuth2Permissiongrants["{id}"]
        .Request()
        .UpdateAsync(oAuth2PermissionGrant);
    

    注意: 在 patch 命令中添加您已授予访问权限并希望授予访问权限的范围。 如果您仅添加要授予访问权限的范围,则先前的范围授予访问权限将被撤销,并且只有新角色将具有授予访问权限。

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 2017-09-08
      • 2018-07-22
      • 2019-05-21
      • 1970-01-01
      • 2019-05-16
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多