Microsoft Graph SDK for Java-InvalidAuthenticationToken错误80049217
我的授权客户端:Angular,资源服务器:Java Spring Boot,授权服务器:Azure Active Directory 我使用oAuth2通过PKCE授权流通过Angular登录,然后将令牌传递到后端。 我可以通过Authorization Beaer标头访问后端中的令牌,但当我使用该令牌访问Microsoft Graph API时,我收到一个无效令牌异常 com.microsoft.graph.http.GraphServiceException:错误代码:InvalidAuthenticationToken 错误消息:CompactToken解析失败,错误代码:80049217 我不确定它为什么会导致此错误,因为它是有效的,我可以通过 并使用令牌访问postman中的其他受保护api AuthProvider.javaMicrosoft Graph SDK for Java-InvalidAuthenticationToken错误80049217,java,angular,azure-active-directory,microsoft-graph-api,Java,Angular,Azure Active Directory,Microsoft Graph Api,我的授权客户端:Angular,资源服务器:Java Spring Boot,授权服务器:Azure Active Directory 我使用oAuth2通过PKCE授权流通过Angular登录,然后将令牌传递到后端。 我可以通过Authorization Beaer标头访问后端中的令牌,但当我使用该令牌访问Microsoft Graph API时,我收到一个无效令牌异常 com.microsoft.graph.http.GraphServiceException:错误代码:InvalidAut
public class AuthProvider implements IAuthenticationProvider {
private String accessToken = null;
public AuthProvider(String accessToken) {
this.accessToken = accessToken;
}
@Override
public void authenticateRequest(IHttpRequest request) {
// Add the access token in the Authorization header
request.addHeader("Authorization", "Bearer " + accessToken);
}
}
SecurityConfiguration.java
http.cors().and()
.authorizeRequests().antMatchers("/home").permitAll()
.and()
.authorizeRequests().antMatchers("/actuator/health").permitAll()
.and()
.authorizeRequests().antMatchers("/**").authenticated()
.and()
.oauth2ResourceServer().jwt();
GraphAPIController.java
private static IGraphServiceClient graphClient = null;
private static AuthProvider authProvider = null;
private static void ensureGraphClient(String accessToken) {
if (graphClient == null) {
// Create the auth provider
authProvider = new AuthProvider(accessToken);
// Create default logger to only log errors
DefaultLogger logger = new DefaultLogger();
logger.setLoggingLevel(LoggerLevel.ERROR);
// Build a Graph client
graphClient = GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.logger(logger)
.buildClient();
}
}
@GetMapping("/getUser")
public static User getUser(@RequestHeader(value="Authorization") String token) {
System.out.println("THE TOKEN: " +token);
ensureGraphClient(token);
// GET /me to get authenticated user
User me = graphClient
.me()
.buildRequest()
.get();
System.out.println("THE USER: " + me);
return me;
}
我的角度设置:
应用程序模块:
从'angular-oauth2-oidc'导入{OAuthModule};
应用程序组件.ts
邮递员:
访问令牌只能用于一个资源。我可以看到您在角度设置中配置了
scope:'openidapi://{appid}/app'
。这意味着访问令牌用于此资源api://{appid}/app
,而不是Microsoft Graphhttps://graph.microsoft.com
。这就是为什么出现InvalidAuthenticationToken错误
如果你想在后端API中调用微软图形,你需要考虑。OAuth 2.0代表流(OBO)服务于应用程序调用服务/web API的用例,而服务/web API又需要调用另一个服务/web API
在您的情况下,您的后端API是web API A,Microsoft Graph是web API B
A供您参考。您可以使用此令牌访问哪些“其他受保护api”?你能提供这个请求吗?此外,请分享关于如何获得访问令牌的代码。@AllenWu我在附加代码中添加了您的答案。如果对您有帮助,您可以将其作为答案接受(单击答案旁边的复选标记,将其从灰色切换为填充)。看见这可能对其他社区成员有益。谢谢。嗨,你有机会了解我的答案吗?有更新吗?谢谢你的帮助!这正是我想要的。我将代表流程来实现