使用Google OpenID进行Android身份验证.接下来是什么?

The*_*nny 10 openid android

我不是程序员,但我需要自己做.我需要一些帮助.

我一直在寻找最近两天的解决方案,我找不到任何解决方案.

好.我正在编写Android Native App.我的第一个目标是通过Google帐户(已经在手机上设置)实现登录的可能性.

所以我使用AccountManager来获取"com.google"帐户,我这样获得了一个身份验证令牌:

Account[] mAccounts = mAccountManager.getAccountsByType("com.google"); 
AccountManagerFuture<Bundle> response = 
    mAccountManager.getAuthToken(mAccounts[0], "android", null, this, null, null);

Bundle authTokenBundle;
String authToken;

try {
    authTokenBundle = response.getResult();
    authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN).toString();
} catch (OperationCanceledException e) {
    Log.e(TAG, e.getMessage());
} catch (AuthenticatorException e) {
    Log.e(TAG, e.getMessage());
} catch (IOException e) {
    Log.e(TAG, e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

我的问题是 - 我的下一步应该是什么?我如何进一步了解此身份验证过程?我该如何使用此令牌?

我找到了一些资源,但大多数都使用OAuth或基于网络.我只需要进行身份验证并(如果可能)获取用户的名称(我已经有电子邮件地址),我不需要访问任何Google服务.

先感谢您.

Dar*_*tle 8

实际上,OAuth 2是您想要的,而不是OpenID - OpenID本质上是基于Web的,所以您需要通过WebView浏览器跳过一些环节.OAuth 2允许您直接从应用程序使用AccountManager中的令牌和Google API.

在您的通话中getAuthToken(),authTokenType参数是您要成为的OAuth 2范围userinfo.profileuserinfo.email验证电子邮件地址(您已经拥有它,但您尚未验证它;它理论上可能是欺骗性的)并获取名称用户.

这是我在类似情况下用于全范围的内容:

private static final String OAUTH2_SCOPE =
    "oauth2:" +
    "https://www.googleapis.com/auth/userinfo.profile" +
    " " +
    "https://www.googleapis.com/auth/userinfo.email";
Run Code Online (Sandbox Code Playgroud)

当然,您可以使用整个字符串文字内联,但我更喜欢将其构建并清晰,并且如果需要,可以更轻松地进行更改.

在我的情况下,我使用getAuthTokenByFeatures(),像这样:

am.getAuthTokenByFeatures("com.google", OAUTH2_SCOPE, null, this, null, null,
                          new AccountManagerCallback<Bundle>()
{
    public void run(AccountManagerFuture<Bundle> future) {
        try {
            Bundle bundle = future.getResult();
            System.out.println("Got Bundle:\n" +
                               " act name: " +
                               bundle.getString(AccountManager.KEY_ACCOUNT_NAME) +
                               "\n act type: " +
                               bundle.getString(AccountManager.KEY_ACCOUNT_TYPE) +
                               "\n auth token: " +
                               bundle.getString(AccountManager.KEY_AUTHTOKEN));
        } catch (Exception e) {
            System.out.println("getAuthTokenByFeatures() cancelled or failed:");
            e.printStackTrace();
        }
    }
}, null);
Run Code Online (Sandbox Code Playgroud)

但您可以将相同的想法应用于您的代码.然后,您可以将OAuth令牌与Google用户信息API一起使用,如使用OAuth 2.0 for Login验证电子邮件并获取用户名称中所述.