Nam Vu
Nam Vu

Reputation: 5725

How can i log out with AndroidAccountManager

i can login with Android AccountManager with this code: http://code.google.com/p/google-api-java-client/wiki/AndroidAccountManager

But i don't know how to log out?

Upvotes: 4

Views: 3586

Answers (3)

Android-Christensen
Android-Christensen

Reputation: 19

AccountManager clearPassword (Account account)

From the AccountManager:

Forgets a saved password. This erases the local copy of the password; it does not change the user's account password on the server. Has the same effect as setPassword(account, null) but requires fewer permissions, and may be used by applications or management interfaces to "sign out" from an account.

Upvotes: 2

Vlad Gurovich
Vlad Gurovich

Reputation: 8463

Why not just invalidate your current auth token by calling

 AccountManager.getInstance().invalidateAuthToken(accountType, currentToken);

AccountManager.invalidateAuthToken Documentation

Upvotes: 3

jcwenger
jcwenger

Reputation: 11453

You don't. When you use the built-in android authentication, you authenticate using the username and password that the user has provided in the "Accounts and Sync" control panel. Once you have that authentication, you use it to obtain an auth-token which you should cache and use until it goes bad.

So, let's go with the way you access google services using a "com.google" style account. You will end up authenticating using AccountManager, when your app wants to sync (You should definitely be using a SyncAdapter to do this). Once you authenticate, you will get an auth-token. this is a big string of random letters, which acts as a "key" on subsequent web calls. You will save this, and as long as it's good, you won't need to authenticate again. So, want go to fetch... let's say, a google finance portfolio. You include the auth-token as part of the http get header. One of two things happens:

  1. Google likes that token and uses it (in place of of a username/password pair) to authenticate you, and returns data for your request.
  2. Google returns http error 40x indicating that the auth-token youp provided is no good.

The latter case will happen for two reasons:

  1. It's too old. Google likes you to re-authenticate periodically (just like you have to enter your password at gmail.com every week or two to make sure it's still you.) When this happens, Invalidate the auth token (There's a function call) and get a new one.
  2. The user has changed their password since you got the auth token. Same thing, except along the way, the device will fail to authenticate, and then will complain to the user that they need to re-enter their password, and until they do, you'll either get a NULL auth token, or the call will block until they get a password (Depending on which function call you use to get the token).

In any case, you never log out. You just use the service with the auth token you've obtained and cached, until you don't anymore. Think of the auth token you get as being like a session key that stays good for as long as you're using it.

Upvotes: 4

Related Questions