[英]How to get Microsoft graph access token from java spring
我试图从邮递员UI中获取图形API令牌,并且能够获取计划程序数据。 如何在Java Spring中实现相同
我无法使用Java Spring获得Microsoft图形API的访问令牌。 我可以使用邮递员获得访问令牌。
我需要从Web应用程序之一访问规划器API。 根据Microsoft文档,我在azure活动目录中配置了一个应用程序,并获取了客户端密钥,秘密密钥等。我还配置了获取组和用户所需的权限。
第一次我从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
我获得了令牌,并且能够从https://graph.microsoft.com/v1.0/groups/获取组
但是,相同的令牌对于获取小组计划无效。
经过大量的挖掘,我知道用client_credentials访问的令牌不适用于从计划程序API获取数据。 因此,接下来我使用以下详细信息从邮递员的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>
我得到了Microsoft登录屏幕,成功登录后,我获得了令牌。 我可以使用此访问令牌调用计划程序API。
现在我的问题是如何使用java spring获得相同的令牌。 另外,我的Web应用程序每天都会在调度程序中调用图形API来运行后台服务。 我不想在这里进行人工干预,但是如前所述,graph 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;
}
干得好! 编写此代码是为了获得应用程序许可(因此不委托)。 它只需要您的客户ID和密码即可操作。 您将需要Microsoft图形罐使其工作(以及支持它的许多罐)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.