|
5 | 5 | import com.microsoft.aad.msal4j.ClientCredentialParameters; |
6 | 6 | import com.microsoft.aad.msal4j.ConfidentialClientApplication; |
7 | 7 | import com.microsoft.aad.msal4j.IAuthenticationResult; |
| 8 | +import com.microsoft.aad.msal4j.IClientCredential; |
| 9 | +import com.microsoft.aad.msal4j.MsalException; |
8 | 10 | import com.microsoft.aad.msal4j.SilentParameters; |
9 | 11 |
|
10 | 12 | import java.util.Collections; |
11 | | -import java.util.concurrent.CompletableFuture; |
12 | | -import java.util.function.BiConsumer; |
| 13 | +import java.util.Set; |
13 | 14 |
|
14 | 15 | class ClientCredentialGrant { |
15 | 16 |
|
| 17 | + private final static String CLIENT_ID = ""; |
| 18 | + private final static String AUTHORITY = "https://login.microsoftonline.com/<tenant>/"; |
| 19 | + private final static String CLIENT_SECRET = ""; |
| 20 | + private final static Set<String> SCOPE = Collections.singleton(""); |
| 21 | + |
16 | 22 | public static void main(String args[]) throws Exception { |
17 | | - getAccessTokenByClientCredentialGrant(); |
| 23 | + IAuthenticationResult result = acquireToken(); |
| 24 | + System.out.println("Access token: " + result.accessToken()); |
18 | 25 | } |
19 | 26 |
|
20 | | - private static void getAccessTokenByClientCredentialGrant() throws Exception { |
21 | | - |
22 | | - ConfidentialClientApplication app = ConfidentialClientApplication.builder( |
23 | | - TestData.CONFIDENTIAL_CLIENT_ID, |
24 | | - ClientCredentialFactory.createFromSecret(TestData.CONFIDENTIAL_CLIENT_SECRET)) |
25 | | - .authority(TestData.TENANT_SPECIFIC_AUTHORITY) |
26 | | - .build(); |
| 27 | + private static IAuthenticationResult acquireToken() throws Exception { |
27 | 28 |
|
28 | | - ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder( |
29 | | - Collections.singleton(TestData.GRAPH_DEFAULT_SCOPE)) |
30 | | - .build(); |
| 29 | + // Load token cache from file and initialize token cache aspect. The token cache will have |
| 30 | + // dummy data, so the acquireTokenSilently call will fail. |
| 31 | + TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json"); |
31 | 32 |
|
32 | | - CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam); |
| 33 | + // This is the secret that is created in the Azure AD portal |
| 34 | + IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET); |
| 35 | + ConfidentialClientApplication cca = |
| 36 | + ConfidentialClientApplication |
| 37 | + .builder(CLIENT_ID, credential) |
| 38 | + .authority(AUTHORITY) |
| 39 | + .setTokenCacheAccessAspect(tokenCacheAspect) |
| 40 | + .build(); |
33 | 41 |
|
34 | | - BiConsumer<IAuthenticationResult, Throwable> processAuthResult = (res, ex) -> { |
35 | | - if (ex != null) { |
36 | | - System.out.println("Oops! We have an exception - " + ex.getMessage()); |
37 | | - } |
38 | | - System.out.println("Returned ok - " + res); |
39 | | - System.out.println("Access Token - " + res.accessToken()); |
40 | | - System.out.println("ID Token - " + res.idToken()); |
41 | | - }; |
| 42 | + IAuthenticationResult result; |
| 43 | + try { |
| 44 | + SilentParameters silentParameters = |
| 45 | + SilentParameters |
| 46 | + .builder(SCOPE) |
| 47 | + .build(); |
42 | 48 |
|
43 | | - future.whenCompleteAsync(processAuthResult); |
44 | | - future.join(); |
| 49 | + // try to acquire token silently. This call will fail since the token cache does not |
| 50 | + // have a token for the application you are requesting an access token for |
| 51 | + result = cca.acquireTokenSilently(silentParameters).join(); |
| 52 | + } catch (Exception ex) { |
| 53 | + if (ex.getCause() instanceof MsalException) { |
45 | 54 |
|
46 | | - SilentParameters silentParameters = |
47 | | - SilentParameters.builder(Collections.singleton(TestData.GRAPH_DEFAULT_SCOPE)).build(); |
| 55 | + ClientCredentialParameters parameters = |
| 56 | + ClientCredentialParameters |
| 57 | + .builder(SCOPE) |
| 58 | + .build(); |
48 | 59 |
|
49 | | - future = app.acquireTokenSilently(silentParameters); |
50 | | - |
51 | | - future.whenCompleteAsync(processAuthResult); |
52 | | - future.join(); |
| 60 | + // Try to acquire a token. If successful, you should see |
| 61 | + // the token information printed out to console |
| 62 | + result = cca.acquireToken(parameters).join(); |
| 63 | + } else { |
| 64 | + // Handle other exceptions accordingly |
| 65 | + throw ex; |
| 66 | + } |
| 67 | + } |
| 68 | + return result; |
53 | 69 | } |
54 | 70 | } |
0 commit comments