microsoft-graph-api - 使用 Microsoft Graph API - Java 授予管理员许可

标签 microsoft-graph-api azure-ad-b2c msgraph

我已经使用图形 API 创建了一个应用程序,并且我已经为他们分配了权限 - 委托(delegate)和应用程序......

    ServicePrincipal servicePrincipal = graphClient.servicePrincipals(resSerPrinId)
            .buildRequest()
            .get();
    
    List<AppRole> appRoles = servicePrincipal.appRoles;
    List<PermissionScope> scopes = servicePrincipal.oauth2PermissionScopes;
    
    List<ResourceAccess> raList = new ArrayList<ResourceAccess>();
    
    for (AppRole appRole : appRoles) {
        ResourceAccess access = new ResourceAccess();
        access.id = appRole.id;
        access.type = "Role";
        raList.add(access);
    }
    
    System.out.println("Roles added...");
    
    for (PermissionScope permissionScope : scopes) {
        ResourceAccess access = new ResourceAccess();
        access.id = permissionScope.id;
        access.type = "Scope";
        raList.add(access);
    }
    
    System.out.println("Scopes added...");
    
    RequiredResourceAccess reqResAccess = new RequiredResourceAccess();
    reqResAccess.resourceAccess = raList;
    reqResAccess.resourceAppId = resSerPrinAppClientId;
    
    List<RequiredResourceAccess> rraList = new ArrayList<RequiredResourceAccess>();
    rraList.add(reqResAccess);
    
    Application application = graphClient.applications(clientAppObjId)
            .buildRequest()
            .get();
    
    application.requiredResourceAccess = rraList;
    
    graphClient.applications(clientAppObjId)
    .buildRequest()
    .patch(application);

在上面的代码中,resSerPrinId 是资源服务主体 Id,它在 list 中具有应用程序角色,在“公开 api”部分中具有作用域...

所以我从该资源服务主体中提取 appRoles 和 oauth2Permission 并将它们发送给客户端服务主体...

在 UI 中,我看到权限没有授予...

enter image description here

是否可以使用某些图形 API 或手动加载这些权限然后授予他们管理员权限...或者我是否需要始终使用 UI 来执行此操作...?

最佳答案

所以我们基本上有两种类型的权限 - 角色(应用程序)/范围(委托(delegate))

因此,要为您的委派权限提供“授予管理员同意”,请使用以下代码段

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

OAuth2PermissionGrant oAuth2PermissionGrant = new OAuth2PermissionGrant();
oAuth2PermissionGrant.clientId = "clientId-value";
oAuth2PermissionGrant.consentType = "consentType-value";
oAuth2PermissionGrant.principalId = "principalId-value";
oAuth2PermissionGrant.resourceId = "resourceId-value";
oAuth2PermissionGrant.scope = "scope-value";

graphClient.oauth2PermissionGrants()
    .buildRequest()
    .post(oAuth2PermissionGrant);

这是文档的链接 - Read about oAuth2PermissionGrant here

现在要为应用程序权限提供“授予管理员同意”,请使用以下代码片段...

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

AppRoleAssignment appRoleAssignment = new AppRoleAssignment();
appRoleAssignment.principalId = UUID.fromString("33ad69f9-da99-4bed-acd0-3f24235cb296");
appRoleAssignment.resourceId = UUID.fromString("9028d19c-26a9-4809-8e3f-20ff73e2d75e");
appRoleAssignment.appRoleId = UUID.fromString("ef7437e6-4f94-4a0a-a110-a439eb2aa8f7");

graphClient.servicePrincipals("9028d19c-26a9-4809-8e3f-20ff73e2d75e").appRoleAssignedTo()
    .buildRequest()
    .post(appRoleAssignment);

这是文档的链接 - Read more about AppRoleAssignment here

你必须在这里使用这两个坏男孩并完成 - 只需使用正确的 ID(不要将客户端 ID 与客户端主体 ID 等混淆)

基本上流程是 - 获取所有权限(两种类型都使用 Required Resource Access 加载 - 然后使用上面的代码向所有权限添加管理员授权。希望这有帮助。如果您需要更多帮助,请发表评论。

关于microsoft-graph-api - 使用 Microsoft Graph API - Java 授予管理员许可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70005161/

相关文章:

angular - 在 'https://login.microsoftonline.com/xxx from origin ' http ://localhost:4200' has been blocked by CORS policy 访问 XMLHttpRequest

azure - 使用 Microsoft Identity 通过 Core 3.1 Web API 进行客户端 SPA 身份验证

azure - Connect-MSGraph 命令无法在 Azure Functions powershell core 6 中执行

Azure Graph API - 批准 PIM 请求

azure-ad-b2c - 更改密码 Azure AD B2C

c# - 在 Azure AD b2c 中创建新用户错误 : Another object with the same value for property proxyAddresses already exists

Azure B2C : Enable other devs to access B2C Tenant via their Microsoft Accounts

c# - 使用 Ms Graph 更新日历事件在 Start end End 属性中使用了错误的时区