簡體   English   中英

如何在AccountManager中存儲和檢索auth_token

[英]How to store and retrieve auth_token to/from 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();
}

有什么建議么 ?

最后,我可以通過以下代碼段檢索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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM