Android使用Google凭据在我的应用中登录

时间:2010-12-29 09:44:58

标签: android google-account

我正在开发一个应用程序我希望允许用户使用他的Google帐户登录我的服务器,而不会询问他的凭据,因为我知道它会保存到AccountManager类中。 问题是我不知道如何对用户进行验证,因为帐户ID只存储在Google的服务器中,那么我在哪里可以找到该帐户的ID以及如何使用它来验证用户?我想按照以下步骤操作:用户选择一个存储的帐户,我得到一个id,从终端发送到服务器,我回答“他信任”这个用户,之后我将这个id保存在我的服务器并向Google请求身份验证令牌,以便在我的服务器和Android设备上的应用程序之间使用它。

1 个答案:

答案 0 :(得分:4)

这是你想要的吗?

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);
  }
}

您可以在Google开源的ChromeToPhone应用代码中看到更详细的示例:http://www.google.com/codesearch/p?hl=en#JWblrwroAxw/trunk/android/src/com/google/android/apps/chrometophone/MainActivity.java&l=311