Android login in my app with google credentials

g.geloso picture g.geloso · Dec 29, 2010 · Viewed 7.1k times · Source

HI I'm developing an application in wich I want to permit the user to login to my server with his Google account without asking him the credentials because I know that are saved into the AccountManager class. The problem is that I don't know how to autenticate the user because the account ID is just stored in Google's server, so Where can I find an ID for the account and how can I use it to autenticate user? I'd like to follow these steps: user choose one of the stored account, I get an id, send it from the terminal to the server, and I answere that "he trust" this user, after this I save this id in my server and request an auth token to Google just to use it between my server and the application on android device.

Answer

Ozone picture Ozone · Dec 29, 2010

Is this what you want?

List<String> googleAccounts = new ArrayList<String>();
Account[] accounts = AccountManager.get(this).getAccounts();
for (Account account : accounts) {
  if (account.type.equals("com.google")) {
    googleAccounts.add(account.name);
  }
}

You can see a more detailed example in the code of the ChromeToPhone app open-sourced by Google: http://www.google.com/codesearch/p?hl=en#JWblrwroAxw/trunk/android/src/com/google/android/apps/chrometophone/MainActivity.java&l=311