Android bootstrap:需要将authToken传递给我的请求接受标头

时间:2012-12-17 20:00:20

标签: android rest authentication

好吧,我一直在努力用这个“模板”project来理解这个身份验证过程,但是没有得到它。

我想要做的是一个基本的登录,所以我可以获得我的授权令牌,我以前通过使用SharedPreference(从未实际使用AccountManager)并从我的其余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);
}

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

1 个答案:

答案 0 :(得分:3)

我不确定为什么setAccountAuthenticatorResult(...)没有正确应用authtoken,但我很幸运,在accountManager.setAuthToken(...)

中添加了以下finishLogin()来电
if (authTokenType != null
            && authTokenType.equals(Constants.Auth.AUTHTOKEN_TYPE)) {
        intent.putExtra(KEY_AUTHTOKEN, authToken);
        accountManager.setAuthToken(account, authTokenType, authToken);
}

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