gpt4 book ai didi

android - AccountManager.blockingGetAuthToken() 不提示输入凭据

转载 作者:行者123 更新时间:2023-11-29 01:28:43 25 4
gpt4 key购买 nike

我在我的应用程序中使用 SyncAdapter 和 AccountAuthenticator。在执行同步操作时,我调用 AccountManager.blockingGetAuthToken 来获取访问 token 。我是这样理解这个方法的,当它无法获得 token 时(或者换句话说,当 getAuthToken 方法返回一个 Intent 以启动 Activity 时),它会启动我的登录 Activity 。但它只是返回 null,而没有启动 Activity。

这是我的身份验证器的 getAuthToken 方法。

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {

// Extract the username and password from the Account Manager, and ask
// the server for an appropriate AuthToken.
final AccountManager am = AccountManager.get(mContext);

String authToken = am.peekAuthToken(account, authTokenType);

// Lets give another try to authenticate the user
if (TextUtils.isEmpty(authToken)) {
final String password = am.getPassword(account);
if (password != null) {
try {
authToken = APIHelper.getInstance().logIn(account.name, password);
} catch (IOException e) {
e.printStackTrace();
}
}
}

// If we get an authToken - we return it
if (!TextUtils.isEmpty(authToken)) {
// cache
am.setAuthToken(account, authTokenType, authToken);
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;
}

// If we get here, then we couldn't access the user's password - so we
// need to re-prompt them for their credentials. We do that by creating
// an intent to display our AuthenticatorActivity.
final Intent intent = new Intent(mContext, AuthActivity.class);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
intent.putExtra(AuthActivity.ARG_ACCOUNT_TYPE, account.type);
intent.putExtra(AuthActivity.ARG_AUTH_TYPE, authTokenType);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}

顺便说一句,大部分代码来自 this blog.

最佳答案

看来提问者想通了。对于后代,如果您改用它,它会起作用:

AccountManagerFuture<Bundle> resultFuture = accountManager.getAuthToken(
account,
AUTH_TOKEN_TYPE,
null,
activity,
null,
null
);
Bundle bundle = resultFuture.getResult();
return bundle.getString(AccountManager.KEY_AUTHTOKEN);

我猜 blockingGetAuthToken() 无法自动执行此操作,因为它缺少 activity 参数。而且文档不正确。

关于android - AccountManager.blockingGetAuthToken() 不提示输入凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32442071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com