使用 Graph Api 对电子邮件使用更改通知。面临错误,如“在缓存中找不到令牌”

Using Graph Api to use change notification for emails. facing error as " Token not found in the cache"

提问人:Pankaj Patil 提问时间:5/17/2023 最后编辑:Pankaj Patil 更新时间:5/17/2023 访问量:94

问:

我想在我的 java 程序中获取 Microsoft 电子邮件通知。我正在使用 Microsoft Graph API 的更改通知,以便在收到新电子邮件时收到通知。 我有秘密 ID、值、client_id和tenant_id等信息。

我已经编写了一个代码来订阅电子邮件,但遇到了问题。

法典:


    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.
java 电子邮件 microsoft-graph-mail 更改通知

评论


答: 暂无答案