Developers, I need a solution to read outlook mail with springboot by client_secret. I tried with multiple process but no luck. When I debug I’ve access token but refresh token, id token size were 0. Is this causing me issue below,
Error message: Access is denied. Check credentials and try again.
GET https://graph.microsoft.com/v1.0/users/[email protected]/mailFolders/inbox/messages?%24top=5
SdkVersion : graph-java/v4.1.0
403 : Forbidden
[…]
[Some information was truncated for brevity, enable debug logging for more details]
public static void initializeGraph() throws MalformedURLException, ExecutionException, InterruptedException {
Set<String> SCOPE = new HashSet<>();
SCOPE.add("https://graph.microsoft.com/.default");
SCOPE.add("offline_access");
IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
ConfidentialClientApplication cca = ConfidentialClientApplication
.builder(CLIENT_ID, credential)
.authority(AUTHORITY)
.build();
IAuthenticationResult result;
ITokenCache tokenCache = cca.tokenCache();
try {
SilentParameters silentParameters = SilentParameters
.builder(SCOPE)
.build();
// Try to acquire token silently
result = cca.acquireTokenSilently(silentParameters).get();
System.out.println(result);
} catch (Exception ex) {
if (ex.getCause() instanceof MsalException) {
ClientCredentialParameters parameters = ClientCredentialParameters
.builder(SCOPE)
.build();
// Try to acquire a token
IAuthenticationResult accessToken = cca.acquireToken(parameters).get();
System.out.println("Token retrieved from accessToken: " + accessToken.accessToken());
System.out.println("Token retrieved from idToken: " + accessToken.idToken());
System.out.println("Token retrieved from tenantProfile: " + accessToken.tenantProfile());
System.out.println("Token retrieved from expiresOnDate: " + accessToken.expiresOnDate());
TokenCredential tokenCredential = new TokenCredential() {
@Override
public Mono<com.azure.core.credential.AccessToken> getToken(TokenRequestContext tokenRequestContext) {
String accessToken = accessToken.accessToken();
Instant instant = accessToken.expiresOnDate().toInstant();
com.azure.core.credential.AccessToken azureToken = new
com.azure.core.credential.AccessToken(accessToken,
instant.atOffset(ZoneId.systemDefault().getRules().getOffset(instant)));
return Mono.just(azureToken);
}
};
IAuthenticationProvider authProvider = new TokenCredentialAuthProvider(tokenCredential);
userClient = GraphServiceClient
.builder()
.authenticationProvider(authProvider)
.buildClient();
} else {
throw ex;
}
}
}
public static MessageCollectionPage getInbox() throws Exception {
initializeGraphForAppOnlyAuth();
System.out.println(userClient);
if (userClient == null) {
throw new Exception("Graph has not been initialized for user auth");
}
return userClient.users("[email protected]")
.mailFolders("inbox")
.messages()
.buildRequest()
.top(5)
.get();
}
public static MessageCollectionPage getInbox() throws Exception {
initializeGraphForAppOnlyAuth();
System.out.println(userClient);
if (userClient == null) {
throw new Exception("Graph has not been initialized for user auth");
}
return userClient.users("[email protected]")
.mailFolders("inbox")
.messages()
.buildRequest()
.top(5)
.get();
}
Rishi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.