Office 365 v2 API授权代码格式错误或无效

时间:2017-02-06 18:01:03

标签: azure office365 office365-restapi azure-ad-graph-api adal4j

我为从授权我们的应用使用Office 365电子邮件的用户在浏览器中复制了以下身份验证码。

code=OAQABAAIAAADRNYRQ3dhRSrm-4K-adpCJ3J3UJ8GyC2qJDvNhlrUAObjph6sQ3A9waeQ5Tr-DA6WzxCdFbvadCRJw2S4a_lwA7MyelZWAPQZOlaB_X_1165CbmTXJMGioU6Cr0DhVTUzIlUv_-Svjp8DBrLVCxcDp5rJMM5mDNR0iGysuDIozWnOaPqCOl35NxPzyktrYK6D1MBptmXOPbhS-stTZXbHJr9gGE3FHzMU0XANXmTm30q4SPaoWPch-S1uFFL4xwS2oUv-lELBdcfIGh5UJBSraabGihVWUnbwBhh8eURSMRwryi7kubUcq0D27S-vIVZhtKopemQ1njAcExO58S7EgAyqbIzMxvmBXBe0X1ieVrcyHYRpt4ZAq1Z4v5HLTrYhx5fGp6AkqhV09yri3bqXaZvw5R1hKuhAbRDt_isZn_L8ZEhfwnqICGUwpDU27c6Qd1txuiOVY90a4BiAUh1M1u5gjDx8nIE88R7S915w7mUjJtCzZuTKQavve8q8UOtm9udUvBOX1f-bYslpgiIRbdSYBYlP9UrbreLS1W6OFk2NX-uqp9mabyImvvj1RUm166qV6uc9hsuhzrfErDURC17JotuQBSWYauAvb38p5B-cDbsCZafpyORlbrWsYyQcdWwUPL0aOZEQXFW-v3gDw7Xri_9hvsiHrj10NTaaozqm1QpZmMf-SHJ0yF9wBWKYgAA

如果我们使用Microsoft Graph REST API v1,应用程序可以正常运行但是在使用版本2时会出现以下问题。它使用委托权限注册,授予我们读取,写入/发送权限,该权限适用于应用程序的V1

对于V2:authority =https://login.microsoftonline.com/common/oauth2/v2.0/token和 要检索身份验证代码我使用以下网址

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=30..7&response_type=code&scope=mail.read&redirect_uri=https://myurl:8443/controller/saveToken

导致问题的代码块:

 @Override
    public AuthenticationResult getToken(String authCode) {

        ExecutorService service = Executors.newFixedThreadPool(1);
        OfficeCredentials credentials = getCredentials();

        try {
            AuthenticationContext context = new AuthenticationContext(credentials.getAuthority(), true, service);
            final Future<AuthenticationResult> resultFuture = context.acquireTokenByAuthorizationCode(
                    authCode, new URI(credentials.getRedirectUri()), new ClientCredential(credentials.getClientId(),
                            credentials.getClientSecret()), credentials.getResourceUrl(), null);

            return resultFuture.get();//throws exception

        } catch (URISyntaxException e) {
            logger.error(e.getMessage());
        } catch (MalformedURLException e) {
            logger.error(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());

        }

        return null;

    }

调用 resultFuture.get()时出现异常

java.util.concurrent.ExecutionException: com.microsoft.aad.adal4j.AuthenticationException: {"error_description":"AADSTS70000: Transmission data parser failure: Authorization Code is malformed or invalid.\r\nTrace ID: c37b4aba-c5fb-44f3-815c-dd798072095d\r\nCorrelation ID: e190ccd2-f98a-440c-8e79-69cfcead3c04\r\nTimestamp: 2017-02-06 17:53:30Z","error":"invalid_grant"}

当我尝试迁移到v2时,我不知道自己做错了什么。 redirect_uri与azure中定义的相同,它是HTTPS。我已经通过关注this让我的本地环境接受了HTTPS。仅供参考:我使用的是adal4 java库。

1 个答案:

答案 0 :(得分:2)

目前,adal4j库不支持Azure AD v2.0端点(请参阅here)。事件我们设置v2.0端点的权限,它仍然使用旧的。

作为解决方法,您可以直接撰写HTTP请求。以下是您参考的示例请求(请参阅here):

POST: https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token

client_id={clientId}&client_secret={clientSecret}&scope={scope}&code={authorizationCode}&grant_type=authorization_code&redirect_uri={redirectUri}

如果您希望adal4j库支持Azure AD v2.0端点,则可以从here提交反馈。