Android上的Microsoft ADAL身份验证

时间:2017-05-08 05:46:28

标签: android azure adal android-syncadapter

我正在尝试为Microsoft日历创建SyncAdapter,第一步是Authentication。我正在使用com.microsoft.aad:adal:2.0.4-alpha并使用此代码进行首次身份验证:

getAuthenticationContext().acquireToken(
                mContextActivity,
                Constants.SCOPES.split(" "),
                null,
                Constants.CLIENT_ID,
                Constants.REDIRECT_URI,
                PromptBehavior.Auto,
                new AuthenticationCallback<AuthenticationResult>() {
                    @Override
                    public void onSuccess(final AuthenticationResult authenticationResult) {
                        if (authenticationResult != null && authenticationResult.getStatus() ==
                                        AuthenticationResult.AuthenticationStatus.Succeeded) {
                            dependencyResolver = new ADALDependencyResolver(
                                    getAuthenticationContext(),
                                    resourceId,
                                    Constants.CLIENT_ID);
                            token = authenticationResult.getToken();
                            UserInfo userInfo = authenticationResult.getUserInfo();
                            if (userInfo != null) {
                                userIdentifier = new UserIdentifier(userInfo.getUniqueId(),
                                                            UserIdentifier.UserIdentifierType.UniqueId);
                            }
                        }
                    }

                    @Override
                    public void onError(Exception t) {
                        Log.e("initialize", "onError : " + t.getMessage());
                        result.setException(t);
                    }
                }
            );

这完全有效,在输入用户名和密码后,我可以获得令牌。

这适用于sync adapter,在某些时候我需要静默获取令牌。所以我使用了这段代码:

public void getTokenSilent() {

    getAuthenticationContext()
            .acquireTokenSilent(Constants.SCOPES.split(" "),
                                Constants.CLIENT_ID,
                                userIdentifier,
                                new AuthenticationCallback<AuthenticationResult>() {
                                    @Override
                                    public void onSuccess(
                                            AuthenticationResult authenticationResult) {
                                        UserInfo userInfo = authenticationResult.getUserInfo();
                                    }

                                    @Override
                                    public void onError(Exception e) {
                                        Log.e("getTokenSilent", "onError : " + e.getMessage());
                                    }
                                });
}

执行此代码后,我收到错误:

AUTH_REFRESH_FAILED_PROMPT_NOT_ALLOWED Prompt is not allowed and failed to get token: ver:2.0.4-alpha
onError : Refresh token is failed and prompt is not allowed

如何解决此错误并以静默​​方式获取或刷新令牌?

提前tnx。

0 个答案:

没有答案