1

我正在使用 Google Auth 登录并获取个人资料详细信息。

但我面临一个问题。onActivityResult如果用户第一次进行身份验证,sdk 将有助于添加新帐户,一旦成功,它将重新调用

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
GoogleSignInAccount acct = result.getSignInAccount();

acct.getGivenName()acct.getFamilyName()null。_

但是,如果我使用已经添加的帐户进行身份验证,acct.getGivenName()并且acct.getFamilyName()具有预期价值。

我的代码如下

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestId()
                .requestProfile()
                .build();

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(mActivity)
                .enableAutoManage(mActivity, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
        startActivityForResult(signInIntent, REQUEST_CODE_GOOGLE_SIGN_IN);
4

1 回答 1

0

我在同一个问题上苦苦挣扎了几个小时,但在互联网上没有找到任何回复。经过大量盲目猜测而没有结果,我偶然发现了一个似乎可以解决问题的方法:

我在我的开发者控制台中创建了一个“Web 应用程序”OAuth 2.0 客户端 ID,并在初始化 GoogleSignInOptions 项时传递了它,如下所示:

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(%Your Web application ID here%)
            .requestServerAuthCode(%Your Web application ID here%)
            .requestId()
            .requestEmail()
            .requestProfile()
            .build();

从那时起,它似乎工作得很好。

于 2017-01-06T15:15:38.427 回答