1

好吧,我一直在努力理解这个“模板”项目的身份验证过程,只是不明白。

我正在尝试做的是基本登录,所以我可以获得我的授权令牌,我之前已经通过使用 SharedPreference(从未实际使用过 AccountManager)并从我的 rest android 客户端(通过自定义 SessionManager)访问它来完成它。

到目前为止,我已经完成了第一部分。我能够获得我的授权令牌。

BoostrapAuthenticatorActivity.java:

 /**
     * Called when response is received from the server for authentication
     * request. See onAuthenticationResult(). Sets the
     * AccountAuthenticatorResult which is sent back to the caller. Also sets
     * the authToken in AccountManager for this account.
     */

    protected void finishLogin() {
        final Account account = new Account(email, Constants.Auth.LZGO_ACCOUNT_TYPE);

        if (requestNewAccount)
            accountManager.addAccountExplicitly(account, password, null);
        else
            accountManager.setPassword(account, password);
        final Intent intent = new Intent();
        authToken = token;
        userToken = user;
        intent.putExtra(KEY_ACCOUNT_NAME, userToken);
        intent.putExtra(KEY_ACCOUNT_TYPE, Constants.Auth.LZGO_ACCOUNT_TYPE);
        if (authTokenType != null
                && authTokenType.equals(Constants.Auth.AUTHTOKEN_TYPE))
            intent.putExtra(KEY_AUTHTOKEN, authToken);
        setAccountAuthenticatorResult(intent.getExtras());
        setResult(RESULT_OK, intent);
        finish();
    }

我如何访问这个值?如果你检查这个类,

BootstrapServiceProvider.java:

@Inject private ApiKeyProvider keyProvider;
@Inject private UserAgentProvider userAgentProvider;

/**
 * Get service for configured key provider
 * <p>
 * This method gets an auth key and so it blocks and shouldn't be called on the main thread.
 *
 * @return bootstrap service
 * @throws IOException
 * @throws AccountsException
 */
public BootstrapService getService() throws IOException, AccountsException {
    return new BootstrapService(keyProvider.getAuthKey(), userAgentProvider);
}

最后,提供者:

ApiKeyProvider.java:

@Inject private Activity activity;
@Inject private AccountManager accountManager;

/**
 * This call blocks, so shouldn't be called on the UI thread
 *
 * @return API key to be used for authorization with a {@link com.android.lzgo.core.LzgoService} instance
 * @throws AccountsException
 * @throws IOException
 */
public String getAuthKey() throws AccountsException, IOException {
    AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthTokenByFeatures(Constants.Auth.BOOTSTRAP_ACCOUNT_TYPE,
            Constants.Auth.AUTHTOKEN_TYPE, new String[0], activity, null, null, null, null);
    Log.d("ApiKeyProvider", "ApiKeyProvider= " + accountManagerFuture.getResult().getString(KEY_AUTHTOKEN));
    return accountManagerFuture.getResult().getString(KEY_AUTHTOKEN);
}

但这给我一个空值!我迷路了!

4

1 回答 1

3

我不确定为什么 authtoken 没有被正确应用setAccountAuthenticatorResult(...),但是我很幸运通过添加以下accountManager.setAuthToken(...)调用finishLogin()

if (authTokenType != null
            && authTokenType.equals(Constants.Auth.AUTHTOKEN_TYPE)) {
        intent.putExtra(KEY_AUTHTOKEN, authToken);
        accountManager.setAuthToken(account, authTokenType, authToken);
}

也许这与不使用 Parse.com 作为我们的后端有关?

于 2013-01-07T21:40:59.047 回答