How to obtain refresh token in Microsoft Authorization Code flow - java sdk

Nikhil Dahake 6 Reputation points
2022-02-04T06:43:23.127+00:00

I am implementing "Login with Microsoft button" and I need to store the refresh token in my database so that I can use that to obtain new access tokens in future. I am trying to do this with Java sdk for microsoft graph.

Edit 1: I actually want to create calendar events using my web application. So, the goal is for the web app to access Graph API without having a signed in user present.

This is what the code looks like:

AuthorizationCode authorizationCode = new AuthorizationCode(httpServletRequest.getParameter("code"));  
        String currentUri = httpServletRequest.getRequestURL().toString();  
      
    IAuthenticationResult result;  
    ConfidentialClientApplication app;  
    try {  
        app = createClientApplication();  

        String authCode = authorizationCode.getValue();  
        Set<String> scopes = new HashSet<String>();  
        scopes.add("Calendars.ReadWrite");  
          
        AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder(authCode, new URI(currentUri)).scopes(scopes)  
                .build();  
          
        Future<IAuthenticationResult> future = app.acquireToken(parameters);  
        result = future.get();  
      
    } catch (ExecutionException e) {  
        throw e.getCause();  
    }  
String accessToken = result.accessToken();  
  
/*  
IAuthenticationResult does not contain any method to get the refresh token - how do I get the refresh token??  
  
I want to do something like: result.refreshToken();  
*/  

IAuthenticationResult is implemented by AuthenticationResult -- but, AuthenticationResult is declared in another class and is not public. AuthenticationResult exposes a method to obtain refreshToken but, I am not able to access it.

Can someone help me access the refresh token - @CarlZhao-MSFT , @James Hamil ?

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,881 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,448 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. Nikhil Dahake 6 Reputation points
    2022-02-04T17:10:44.827+00:00

    See the following links for lead:

    1) https://github.com/AzureAD/microsoft-authentication-library-for-java/issues/228

    2) https://stackoverflow.com/questions/67242530/msal4j-aquire-refresh-token-and-access-token-to-save-instead-of-asking-users-for#comment125393036_67249408

    3)https://github.com/Azure-Samples/ms-identity-java-webapp/blob/d55ee4ac0ce2c43378f2c99fd6e6856d41bdf144/src/main/java/com/microsoft/azure/msalwebsample/AuthHelper.java#L99

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.