微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Outlook OAUTH 2.0 access_token 无法登录 Office 365 帐户

如何解决Outlook OAUTH 2.0 access_token 无法登录 Office 365 帐户

我们正在使用 OAUTH 2.0 和 msal4j 库来实现 access_token,并最终收到了 access_token,但使用该 access_token 我们无法登录 Office 365 帐户。

我们是这个 OAUTH 的初学者,请告诉我们我们错在哪里。

请在下面找到我们使用的代码

          try {
            TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");

            PublicclientApplication pca = PublicclientApplication.builder(clientId)
                    .authority(AUTHORITY)
                    .setTokenCacheAccessAspect(tokenCacheAspect)
                    .build();

            Set<IAccount> accountsInCache = pca.getAccounts().join();
            // Take first account in the cache. In a production application,you would filter
            // accountsInCache to get the right account for the user authenticating.
            IAccount account1 = accountsInCache.iterator().next();
          scopes.add("openid");
          scopes.add("profile");
          scopes.add("User.Read");
          scopes.add("offline_access");
          scopes.add("Mail.Read");

            try {
                SilentParameters silentParameters =
                        SilentParameters
                                .builder( scopes,account1)
                                .build();

                // try to acquire token silently. This call will fail since the token cache
                // does not have any data for the user you are trying to acquire a token for
                result = pca.acquiretokenSilently(silentParameters).join();
            } catch (Exception ex) {
                if (ex.getCause() instanceof MsalException) {

                    InteractiveRequestParameters parameters = InteractiveRequestParameters
                            .builder(new URI("http://localhost"))
                            .scopes(scopes)
                            .build();

                    // Try to acquire a token interactively with system browser. If successful,you should see
                    // the token and account information printed out to console
                    result = pca.acquiretoken(parameters).join();

                } else {
                    // Handle other exceptions accordingly
                    throw ex;
                }

            }


   

            URL url = new URL("https://graph.microsoft.com/v1.0/users");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Authorization","Bearer " + result.accesstoken());
            conn.setRequestProperty("Accept","application/json");

            String response = HttpClientHelper.getResponseStringFromConn(conn);

            int responseCode = conn.getResponseCode();
            if(responseCode != HttpURLConnection.HTTP_OK) {
                throw new IOException(response);
            }

            JSONObject responSEObject = HttpClientHelper.processResponse(responseCode,response);
        }catch (Exception e){
            PrintUtil.printEmailLogFile("exception while acquiring token for outlook");
        }

注意:我们成功收到了 access_token,但是使用这个令牌我们无法连接到 store(imap) 。认证失败

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。