java - 如何从java spring获取Microsoft graph访问 token

标签 java rest spring-boot microsoft-graph-api

我尝试从 postman UI 获取图形 api token ,并且能够获取规划器数据。 如何在java spring中实现相同的目标

我无法使用 java spring 获取 Microsoft graph api 的访问 token 。我可以使用 postman 获取访问 token 。

我需要从其中一个 Web 应用程序访问 Planner API。根据 Microsoft 文档,我在 azure Active Directory 中配置了一个应用程序并获取了客户端 key 、 key 等。 我还配置了获取组和用户所需的权限。

我第一次使用下面的 POSTMAN https://login.microsoftonline.com//oauth2/token 包含以下数据

client_id     : <client_id from configured app> 
client_secret : <client secret from configured app>
grant_type    : client_credentials
resource      : https://graph.microsoft.com

我获得了 token ,并且能够从 https://graph.microsoft.com/v1.0/groups/ 获取群组

但是相同的 token 对于获取团体计划无效。

经过大量挖掘,我发现使用 client_credentials 访问的 token 不适用于从 planner API 获取数据。因此,接下来我使用以下详细信息从 postman 的 UI 获取访问 token 。

Grant Type  : authorization_code
Callback URL : https://www.getpostman.com/oauth2/callback
Auth URL   : https://login.microsoftonline.com/<tenant_id>/oauth2/authorize?resource=https://graph.microsoft.com 
Access Token URL  : https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token

client_id     : <client_id from configured app> 
client_secret : <client secret from configured app>

我看到了Microsoft登录屏幕,登录成功后,我得到了 token 。 我可以使用此访问 token 调用规划器 API。

现在我的问题是如何使用 java spring 获得相同的 token 。 此外,我的网络应用程序将在每天调用图形 API 的调度程序中运行后台服务。 我不想在这里进行手动干预,但正如前面所说,图形 API 会要求登录。

如何实现上述要求。

最佳答案

private String getAuth() {
        ConfidentialClientApplication app = null;

        IClientCredential credential = ClientCredentialFactory.create(Appsecret);
        try {
            app = ConfidentialClientApplication.builder(MicrsoftAppId, credential).authority("https://login.microsoftonline.com/"+tenantId+"/").build();
        }catch(MalformedURLException e) {
            System.out.println(e.getMessage());
        }

        ClientCredentialParameters parameters = ClientCredentialParameters.builder(Collections.singleton("https://graph.microsoft.com/.default")).build();

        CompletableFuture<IAuthenticationResult> future = app.acquireToken(parameters);

        try {
            IAuthenticationResult result = future.get();
            return result.accessToken();
        }catch(ExecutionException e){
            System.out.println(e.getMessage());
        }catch(InterruptedException e) {
            System.out.println(e.getMessage());
        }
        return null;        
    }

给你!此代码是为应用程序权限而编写的(因此未委托(delegate))。它只需要您的客户端 ID 和密码即可操作。您将需要 microsoft graph jar 才能使其工作(以及支持它的许多 jars)。

关于java - 如何从java spring获取Microsoft graph访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56742643/

相关文章:

spring - 带有点(.)的Spring MVC @PathVariable被截断

java - 如果上一个路由没有执行完,则不要启动该路由

java - CORS和错误以及Access-Control-Allow-Origin header 的问题

java - 是否可以创建一个可以向任何具体类添加功能的动态代理?

java - 当 Callable 返回特定结果时停止执行器

java - 错误:Adb connection Error:An existing connection was forcibly closed by the remote host

java - 无法修改 Web API/Java Web App 中的对象

java - 如何在 wsdl 中定义根 SOAP 服务

c# - 我应该在我的 API 上公开接口(interface)而不是具体对象吗?

java - 如何使 spring data jpa 生成的方法在空参数上抛出异常