【问题标题】:Trying to call Microsoft Graph API in Java after retrieving Access token from Angular app从 Angular 应用程序检索访问令牌后尝试在 Java 中调用 Microsoft Graph API
【发布时间】:2020-12-20 16:47:23
【问题描述】:
我曾尝试在网上学习一些示例,但它们并不是很有帮助,而且 Microsoft 官方文档可能有些混乱,而且到处都是。
我有令牌,它在标头中包含随机数。
我在我的 pom.xml 文件中添加了 Microsoft Graph 依赖项,并且一切都设置好了,但我不确定如何使用访问令牌实际实现该方法并让 calle 获取已登录的用户信息例如。
https://graph.microsoft.com/v1.0/me
我已经在 Azure 中设置了我的应用并添加了 API 权限。
任何正确方向的帮助或指导都会有所帮助。
【问题讨论】:
标签:
java
jwt
microsoft-graph-api
【解决方案1】:
您可以参考登录用户请求的示例,请参阅here。
IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
// for "/me" endpoint
User user = graphClient.me().buildRequest().get();
// for "/users/{id | userPrincipalName}" endpoint
// User user = graphClient.users("{id}").buildRequest().get()
用accessToken获取authProvider,参考这个article:
public IGraphServiceClient getAuthProvider() {
IAuthenticationProvider mAuthenticationProvider;
try {
String accessToken = "xxxxxxxxxxxxx";
mAuthenticationProvider = request -> request.addHeader("Authorization", "Bearer " + accessToken);
} catch (Exception e) {
throw new Error("Could not create a graph client: " + e.getLocalizedMessage());
}
return GraphServiceClient.builder()
.authenticationProvider(mAuthenticationProvider)
.buildClient();
}
如需了解更多信息,请Make API calls using the Microsoft Graph SDKs 使用 Java。