我很难理解Android上的身份验证过程。我写了一个自定义验证器:
public class Authenticator extends AbstractAccountAuthenticator {
private final Context mContext;
public Authenticator(Context context) {
super(context);
mContext = context;
}
@Override
public Bundle addAccount(AccountAuthenticatorResponse response,
String accountType,
String authTokenType,
String[] requiredFeatures,
Bundle options) throws NetworkErrorException {
final Intent intent = new Intent(mContext, AuthActivity.class);
intent.putExtra(AuthActivity.KEY_ACCOUNT_TYPE, accountType);
intent.putExtra(AuthActivity.KEY_AUTH_TYPE, authTokenType);
intent.putExtra(AuthActivity.KEY_ADDING_NEW_ACCOUNT, true);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response,
Account account,
String authTokenType,
Bundle options) throws NetworkErrorException {
final AccountManager am = AccountManager.get(mContext);
String authToken = am.peekAuthToken(account, authTokenType);
if (TextUtils.isEmpty(authToken)) {
final String password = am.getPassword(account);
if (password != null) {
//authToken = sServerAuthenticate.userSignIn(account.name, password, authTokenType);
}
} else {
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
return result;
}
final Intent intent = new Intent(mContext, AuthActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
intent.putExtra(AuthActivity.KEY_ACCOUNT_TYPE, account.type);
intent.putExtra(AuthActivity.KEY_AUTH_TYPE, authTokenType);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}
}
我没有写存根方法。
在我尝试注册新帐户后:
AccountManager accountManager = AccountManager.get(MainActivity.this);
Account account = new Account("username", vcs.getAccountType());
accountManager.getAuthToken(account, "oauth", null, MainActivity.this,
new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {}
},
new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
}));
一切正常,我从服务器获得了令牌,但是...如何在身份管理器中从身份验证活动中返回此令牌?
这是我的AuthActivity代码:
public class AuthActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = AuthActivity.class.getSimpleName();
public static final String KEY_ACCOUNT_TYPE = "account_type";
public static final String KEY_AUTH_TYPE = "auth_type";
public static final String KEY_ADDING_NEW_ACCOUNT = "new_account";
public VCS mB = new B();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(mB.getAccessRequest()));
startActivity(browserIntent);
}
@Override
public void onClick(View v) {
}
@Override
protected void onNewIntent (Intent intent) {
Uri uri = intent.getData();
if (uri != null) {
Log.d(TAG, uri.toString());
}
}
}
在onNewIntent
中,我获得了身份验证令牌。