Android google身份验证userinfo.email无效

时间:2012-12-28 12:09:18

标签: android android-2.3-gingerbread google-authentication

我有一个奇怪的问题。我正在使用谷歌身份验证,这适用于Android> 3.X

但是我测试了三星Handled(Android版本2.3.3)和userinfo.email范围不起作用。

oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email

但经过一些试验,我发现了如何让它发挥作用。只需要交换范围:

oauth2:https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile

这一直有效,直到我升级到android 2.3.5版本。在那之后,我无法获得有权抓取用户电子邮件的令牌。我只使用电子邮件尝试交换,但没有任何效果。

我的代码:

private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";

    am.getAuthToken(account, SCOPE, null, this, new AccountManagerCallback<Bundle>() {
        public void run(AccountManagerFuture<Bundle> future) {
            try {
                Bundle result = future.getResult();                 
                String token = result.getString(AccountManager.KEY_AUTHTOKEN);
                Log.d(TAG, "Google tokenas: " + token);


                authWithToken("google", token); //here I get working token, but this token don't have rights to grab user email
            } catch (OperationCanceledException e) {
                unsuccess();
            } catch (AuthenticatorException e) {
                unsuccess();
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, new Handler(new Handler.Callback() {

        public boolean handleMessage(Message msg) {
            Log.d(TAG, "on error: " + msg.what);
            return false;
        }

    }));

此方法仍在使用android 4.0.4和4.1.x。

1 个答案:

答案 0 :(得分:1)

我发现了问题。问题是缓存访问令牌。 Android不检查访问令牌验证。如果访问令牌无效,我只需清除缓存并再次尝试身份验证:

        am.invalidateAuthToken("com.google", token);
        auth(lastAccount);