I want to get the Microsoft email notification in my java program. I am using change notification of Microsoft Graph API to get notified whenever new email gets received. I have information like Secret ID, Value, client_id and tenant_id.
I have written a code to subscribe the emails but getting issues.
Code :
public static void main(String[] args) throws ParseException {
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId("**** client ID ****")
.clientSecret("**** client secret ****")
.tenantId("**** tenantID ****").build();
List<String> scope = new ArrayList<>();
scope.add(".default");
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scope,
clientSecretCredential);
final GraphServiceClient graphClient = GraphServiceClient.builder()
.authenticationProvider(tokenCredentialAuthProvider).buildClient();
final User me = graphClient.me().buildRequest().get();
Subscription subscription = new Subscription();
subscription.changeType = "created,updated";
subscription.notificationUrl = "https://webhook.azurewebsites.net/notificationClient";
subscription.lifecycleNotificationUrl = "https://webhook.azurewebsites.net/api/lifecycleNotifications";
subscription.resource = "/me/mailfolders('inbox')/messages";
subscription.expirationDateTime = OffsetDateTimeSerializer.deserialize("2024-03-20T11:00:00Z");
subscription.clientState = "SecretClientState";
graphClient.subscriptions().buildRequest().post(subscription);
}
}```
After starting the java application, I am getting error as,
15:48:18.633 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
15:48:19.101 [main] DEBUG com.azure.core.implementation.util.Providers - Using com.azure.core.http.okhttp.OkHttpAsyncClientProvider as the default com.azure.core.http.HttpClientProvider.
15:48:19.729 [ForkJoinPool.commonPool-worker-1] DEBUG com.microsoft.aad.msal4j.ConfidentialClientApplication - [Correlation ID: 701314f0-c33f-465b-9102-463be729a515] Execution of class com.microsoft.aad.msal4j.AcquireTokenSilentSupplier failed.
com.microsoft.aad.msal4j.MsalClientException: Token not found in the cache
at com.microsoft.aad.msal4j.AcquireTokenSilentSupplier.execute(AcquireTokenSilentSupplier.java:98)
at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:69)
at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:18)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
15:48:19.740 [ForkJoinPool.commonPool-worker-1] DEBUG com.microsoft.aad.msal4j.AcquireTokenByClientCredentialSupplier - SkipCache set to false. Attempting cache lookup
15:48:19.740 [ForkJoinPool.commonPool-worker-1] DEBUG com.microsoft.aad.msal4j.AcquireTokenByClientCredentialSupplier - Cache lookup failed: Token not found in the cache
15:48:20.043 [ForkJoinPool.commonPool-worker-1] DEBUG com.microsoft.aad.msal4j.ConfidentialClientApplication - [Correlation ID: bcbf5e5d-09f7-4bdd-818f-353fbcfd2ea3] Access Token was returned
15:48:20.044 [ForkJoinPool.commonPool-worker-1] INFO com.azure.identity.ClientSecretCredential - Azure Identity => getToken() result for scopes [.default]: SUCCESS
May 17, 2023 3:48:20 PM com.microsoft.graph.logger.DefaultLogger logError
SEVERE: CoreHttpProvider[sendRequestInternal] - 408Graph service exception
May 17, 2023 3:48:20 PM com.microsoft.graph.logger.DefaultLogger logError
SEVERE: Throwable detail: com.microsoft.graph.http.GraphServiceException: Error code: BadRequest
Error message: /me request is only valid with delegated authentication flow.
GET https://graph.microsoft.com/v1.0/me
SdkVersion : graph-java/v5.54.0
400 : Bad Request
[...]
[Some information was truncated for brevity, enable debug logging for more details]
Exception in thread "main" com.microsoft.graph.http.GraphServiceException: Error code: BadRequest
Error message: /me request is only valid with delegated authentication flow.
GET https://graph.microsoft.com/v1.0/me
SdkVersion : graph-java/v5.54.0
400 : Bad Request
[...]
[Some information was truncated for brevity, enable debug logging for more details]
at com.microsoft.graph.http.GraphServiceException.createFromResponse(GraphServiceException.java:419)
at com.microsoft.graph.http.GraphServiceException.createFromResponse(GraphServiceException.java:378)
at com.microsoft.graph.http.CoreHttpProvider.handleErrorResponse(CoreHttpProvider.java:512)
at com.microsoft.graph.http.CoreHttpProvider.processResponse(CoreHttpProvider.java:442)
at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:408)
at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:225)
at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:202)
at com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:335)
at com.microsoft.graph.requests.UserRequest.get(UserRequest.java:72)
at com.nib.plp.controller.GraphSubscription.main(GraphSubscription.java:37)
Kindly suggest if any changes are required to be made.
Your help is really appreciate as it is extremely important for me to implement.