简体   繁体   English

如何在AccountManager中存储和检索auth_token

[英]How to store and retrieve auth_token to/from AccountManager

I am using AccountManager for storing auth_token. 我正在使用AccountManager存储auth_token。 I am using the following code to store the auth_token. 我正在使用以下代码存储auth_token。 But I am not able to retrieve it back. 但是我无法找回它。

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

And, I am using following code (using Volley library) to retrieve it back, but it's throwing AuthFailureError. 而且,我正在使用以下代码(使用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();
}

Any suggestions ? 有什么建议么 ?

Finally, I could able to retrieve the auth_token through this snippet: 最后,我可以通过以下代码段检索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