为什么我无法多次调用 graphClient?
代码:
public static void initializeGraphAuth(String authorizationCode) {
List<String> scopes = new ArrayList<>();
scopes.add("https://graph.microsoft.com/mail.read");
// Create the auth provider
final AuthorizationCodeCredential authCodeCredential = new AuthorizationCodeCredentialBuilder().clientId(AzureConstants.CLIENT_ID).clientSecret(AzureConstants.CLIENT_SECRET).authorizationCode(authorizationCode) .redirectUrl(AzureConstants.REDIRECT_URI).build();
authProvider = new TokenCredentialAuthProvider(scopes, authCodeCredential);
// 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();
}
public static User getUserDetails() {
return graphClient.me().buildRequest().get();
}
public static List<Group> getUserGroups() {
GroupCollectionPage groups = graphClient.me().transitiveMemberOfAsGroup().buildRequest().get();
return groups.getCurrentPage();
}
在主应用程序中,我调用 getUserDetails() 和 getUserGroups() 方法来分别获取用户详细信息和组详细信息。能够获取用户详细信息但无法获取下面的组详细信息是错误
com.microsoft.aad.msal4j.MsalInteractionRequiredException: AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.
Trace ID: 48d1fee1-cb8b-48c6-a7ec-91e2b2057500
Correlation ID: c58388ec-417c-4398-82ee-68910568f4df
如果我只调用 getUserDetails 或 getUserGroups 中的一个方法,它工作正常,但是当我在代码中调用这两种方法时,它会给出错误
我如何使用 graphClient 对象来获取用户和组的详细信息?
感谢您的帮助
最佳答案
当您的应用程序使用授权代码获取 token 时,这种行为是正常的。
在这种情况下,刷新 token 可用于为其他资源获取额外的 token 。
- 刷新 token 可以在多个资源中使用多次,而授权代码只能使用一次。
- 当 Credential 使用刷新 token 时,它还会在访问 token 过期时更新访问 token 。
您可以通过 Google OAuth2 客户端库使用刷新 token 接收新的访问 token 。
更多相关信息,您可以引用OAuth 2.0 and the Google OAuth Client Library for Java
引用文献:
关于java - OAuth2 授权代码已兑换,请使用新的有效代码重试或使用现有的刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69806282/