AquireToken 回调不起作用
AquireToken Callback Not Working
我的问题:当我从 MainActivity 中取出 adal android aquireToken 调用并将它们放在它们自己的 AuthenticationManager class 中时,回调似乎不起作用。我的 AuthenticationManager class:
中有以下两种方法
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 class 中,我简单地调用了 aquireSharePointTokenSilently 方法,它按预期执行,因为这是第一次登录,它在日志中打印出 "No existing token" 并调用 aquireSharePointToken方法。从那里,回调永远不会参与。例如,如果用户在通过 aquireSharePointToken 方法启动的 ADAL 登录屏幕上点击 "Back" 按钮,则应记录 "Cancelled".
TLDR:aquireSharePointTokenSilently 方法有效,aquireSharePointToken 无效。如果我不使用 AuthenticationManager class 而只是在 MainActivity 中执行这些方法,它们都可以完美地工作。
我感觉这里缺少一些非常基本的东西...
提前致谢。
您是否在 activity 的 onActivityResult 方法中添加了以下行?这是需要的。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
<your Azure AuthenticationContext>.onActivityResult(requestCode, resultCode, data);
}
我的问题:当我从 MainActivity 中取出 adal android aquireToken 调用并将它们放在它们自己的 AuthenticationManager class 中时,回调似乎不起作用。我的 AuthenticationManager class:
中有以下两种方法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 class 中,我简单地调用了 aquireSharePointTokenSilently 方法,它按预期执行,因为这是第一次登录,它在日志中打印出 "No existing token" 并调用 aquireSharePointToken方法。从那里,回调永远不会参与。例如,如果用户在通过 aquireSharePointToken 方法启动的 ADAL 登录屏幕上点击 "Back" 按钮,则应记录 "Cancelled".
TLDR:aquireSharePointTokenSilently 方法有效,aquireSharePointToken 无效。如果我不使用 AuthenticationManager class 而只是在 MainActivity 中执行这些方法,它们都可以完美地工作。
我感觉这里缺少一些非常基本的东西...
提前致谢。
您是否在 activity 的 onActivityResult 方法中添加了以下行?这是需要的。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
<your Azure AuthenticationContext>.onActivityResult(requestCode, resultCode, data);
}