如何从JavaSpring获取MicrosoftGraph访问令牌

如何从JavaSpring获取MicrosoftGraph访问令牌,java,rest,spring-boot,microsoft-graph-api,Java,Rest,Spring Boot,Microsoft Graph Api,我尝试从postman UI获取graph api令牌,并能够获取planner数据。 如何在JavaSpring中实现同样的功能 我无法使用java spring获取Microsoft graph api的访问令牌。我可以使用邮递员获取访问令牌 我需要从一个web应用程序访问planner API。根据Microsoft文档,我在azure active directory中配置了一个应用程序,并获得了客户端密钥、密钥等。 我还配置了获取组和用户所需的权限 这是我第一次用下面的邮递员 /oau

我尝试从postman UI获取graph api令牌,并能够获取planner数据。 如何在JavaSpring中实现同样的功能

我无法使用java spring获取Microsoft graph api的访问令牌。我可以使用邮递员获取访问令牌

我需要从一个web应用程序访问planner API。根据Microsoft文档,我在azure active directory中配置了一个应用程序,并获得了客户端密钥、密钥等。 我还配置了获取组和用户所需的权限

这是我第一次用下面的邮递员 /oauth2/具有以下数据的令牌

client_id     : <client_id from configured app> 
client_secret : <client secret from configured app>
grant_type    : client_credentials
resource      : https://graph.microsoft.com
client\u id:
客户机密:
授予\类型:客户端\凭据
资源:https://graph.microsoft.com
我得到了代币,我可以从

但相同的令牌对于获取组的计划无效

通过大量的挖掘,我了解到使用客户端\ U凭据访问的令牌不适用于从planner API获取数据。所以,接下来我使用下面的详细信息从postman的UI获取访问令牌

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>
授权类型:授权\u代码
回调URL:https://www.getpostman.com/oauth2/callback
验证URL:https://login.microsoftonline.com//oauth2/authorize?resource=https://graph.microsoft.com 
访问令牌URL:https://login.microsoftonline.com//oauth2/v2.0/token
客户识别码:
客户机密:
我获得了Microsoft登录屏幕,成功登录后,我获得了令牌。 我可以使用这个访问令牌调用planner API

现在我的问题是如何使用JavaSpring获得相同的令牌。 此外,我的web应用程序将在调度程序中运行后台服务,每天调用graph API。 我不想在这里手动干预,但正如前面所述,graph API将要求登录

如何实现上述要求。

私有字符串getAuth(){
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;        
    }
机密客户端应用程序app=null; IClientCredential credential=ClientCredentialFactory.create(Appsecret); 试一试{ app=机密客户端应用程序.builder(MicrsoftAppId,凭证).authority(“https://login.microsoftonline.com/“+tenantId+”/”).build(); }捕获(格式错误){ System.out.println(e.getMessage()); } ClientCredentialParameters参数=ClientCredentialParameters.builder(Collections.singleton(“https://graph.microsoft.com/.default))build(); CompletableFuture=app.acquireToken(参数); 试一试{ IAAuthenticationResult=future.get(); 返回result.accessToken(); }捕获(执行例外){ System.out.println(e.getMessage()); }捕捉(中断异常e){ System.out.println(e.getMessage()); } 返回null; }

给你!此代码用于申请权限(因此未授权)。它只需要您的客户Id和密码即可操作。您将需要MicrosoftGraphJAR使其工作(以及支持它的许多jar)

我忘了添加,但请确保您也为应用程序添加了正确的权限