我的问题:当我从MainActivity中拉出adal android aquireToken调用并将其放在自己的AuthenticationManager类中时,回叫似乎不起作用。我的AuthenticationManager类中有以下2种方法:
public void aquireSharePointToken(final Activity rootActivity, final Context applicationContext) {
try {
mAppContext = applicationContext;
mActivity = rootActivity;
mAuthContext= new AuthenticationContext(rootActivity, Constants.TEST_AUTHORITY, true);
mAuthContext.acquireToken(rootActivity, Constants.TEST_SHAREPOINTRESOURCE, Constants.TEST_CLIENTID, Constants.TEST_REDIRECT, PromptBehavior.Auto, new AuthenticationCallback<AuthenticationResult>() {
@Override
public void onSuccess(AuthenticationResult result) {
if (result == null || result.getAccessToken() == null
|| result.getAccessToken().isEmpty()) {
Log.d(TAG, "Token is empty");
} else {
// request is successful
Log.d(TAG, "Status:" + result.getStatus() + " Expired:"
+ result.getExpiresOn().toString());
}
Log.d(TAG, "First Time Token: " + result.getAccessToken());
}
@Override
public void onError(Exception exc) {
if (exc instanceof AuthenticationException) {
Log.d(TAG, "Cancelled");
} else {
Log.d(TAG, "Authentication error:" + exc.getMessage());
}
}
});
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Auth Error");
}
}
public void aquireSharePointTokenSilently(final Activity rootActivity, final Context applicationContext) {
try {
mAppContext = applicationContext;
mActivity = rootActivity;
mAuthContext= new AuthenticationContext(rootActivity, Constants.TEST_AUTHORITY, true);
mAuthContext.acquireTokenSilentAsync(Constants.TEST_SHAREPOINTRESOURCE, Constants.TEST_CLIENTID, getUserId(applicationContext), new AuthenticationCallback<AuthenticationResult>() {
@Override
public void onSuccess(AuthenticationResult result) {
Log.d(TAG,"New Token: " + result.getAccessToken());
}
@Override
public void onError(Exception exc) {
Log.d(TAG, "No existing Token");
aquireSharePointToken(rootActivity,applicationContext);
}
});
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "Auth Error");
}
}
在我的MainActivity类中,我只是调用aquireSharePointTokenSilently方法,并且按预期进行,因为这是第一次登录,因此它在日志中打印出“无现有令牌”,并调用aquireSharePointToken方法。从那里开始,回调永远不会参与。例如,如果用户在通过aquireSharePointToken方法启动的ADAL登录屏幕上时点击“返回”按钮,则它应记录为“已取消”。
TLDR:aquireSharePointTokenSilently方法有效,aquireSharePointToken不起作用。如果我不使用AuthenticationManager类,而只是在MainActivity中执行这些方法,则它们都可以正常工作。
我觉得我在这里错过了一些非常基本的东西...
提前致谢。
最佳答案
您是否在活动的onActivityResult方法中添加了以下行?这是必需的。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
<your Azure AuthenticationContext>.onActivityResult(requestCode, resultCode, data);
}