如何在AccountManager中存储和检索auth_token

时间:2013-12-12 06:01:46

标签: android authentication accountmanager

我正在使用AccountManager来存储auth_token。我使用以下代码来存储auth_token。但我无法取回它。

AccountManager.get(this).addAccountExplicitly(account, "", bundle);

并且,我正在使用以下代码(使用Volley库)将其检索回来,但它正在抛出AuthFailureError。

try {
    Logging.d(LOG_TAG, "AUTH TOKEN : "
            + (new AndroidAuthenticator(this, account,
                    CustomAuthenticator.AUTHTOKEN_TYPE).getAuthToken()));
} catch (AuthFailureError e) {
    Logging.d(LOG_TAG, "AUTH FAILURE");
    e.printStackTrace();
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

最后,我可以通过此代码段检索auth_token:

final AccountManagerFuture<Bundle> future = AccountManager.get(this)
        .getAuthToken(account, authTokenType, null, this, null, null);

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Bundle bnd = future.getResult();
            final String authtoken = bnd
                    .getString(AccountManager.KEY_AUTHTOKEN);
            Logging.d(LOG_TAG, (authtoken != null) ? "SUCCESS!\ntoken: "
                    + authtoken : "FAIL");
        } catch (Exception e) {
            e.printStackTrace();                }
    }
}).start();
相关问题