【问题标题】:How can I avoid user being prompted a second time for Google account selection?如何避免再次提示用户选择 Google 帐户?
【发布时间】:2013-07-17 13:46:08
【问题描述】:

我正在使用 Google Play 游戏服务开发 Android 应用程序,我遵循 getting started guide 将 BaseGameUtils 作为模块导入到我的 IntelliJ 项目中。这现在工作正常,除了每当我打开一个继承 BaseGameActivity 并调用 beginUserInitiatedSignIn() 的活动时,都会提示用户选择要连接的 Google 帐户(如果手机上有多个帐户)。现在,如果不是因为在我的应用程序中,用户已经通过 Google Play 服务using OAuth2 (GoogleAuthUtil/AccountPicker) 进行了身份验证,这会很好。它已经给出了他/她想要使用的帐户 - 不需要再次询问。

无论如何我可以修改BaseGameActivity.javaGameHelper.java 以便不再提示用户吗?和/或我是否需要更改使用 OAuth2 登录的方式?

我尝试将四个范围 PLUS_PROFILE、PLUS_LOGIN、GAMES 和 APP_STATE 添加到我的 OAuth 登录例程中,但 AccountPicker-popup sill 出现在我从 BaseGameActivity 继承的活动中。

更新:Logcat-log

07-23 11:45:25.275: DEBUG/CSDN-GMS(22315): isGooglePlayServicesAvailable returned 0
07-23 11:45:25.275: DEBUG/CSDN-GMS(22315): beginUserInitiatedSignIn: starting new sign-in flow.
07-23 11:45:25.285: DEBUG/CSDN-GMS(22315): Connecting GamesClient.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): onStart.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): onStart: connecting clients.
07-23 11:45:25.295: DEBUG/CSDN-GMS(22315): Connecting GamesClient.
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): onConnectionFailed: result 4
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): onConnectionFailed: since user initiated sign-in, trying to resolve problem.
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): resolveConnectionResult: trying to resolve result: ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4190b780: android.os.BinderProxy@4190a780}}
07-23 11:45:25.395: DEBUG/CSDN-GMS(22315): result has resolution. Starting it.

【问题讨论】:

    标签: android google-play-services google-play-games


    【解决方案1】:

    您需要先使用 OAuth2 登录,还是先登录 Google 游戏?如果您可以先登录 Google 游戏,您可以使用 GamesClient.getCurrentAccountName()。来自https://developer.android.com/reference/com/google/android/gms/games/GamesClient.html#getCurrentAccountName()

    获取当前所选帐户的名称。这是用户选择用于 Google Play 游戏的帐户。

    然后您可以将该帐户选择提供给 OAuth2 身份验证。

    【讨论】:

    • 这听起来像是一个很有前途的方法。这个上下文中的“AccountName”是GMail地址吗? (从文档中不清楚)
    • 这是个好问题。我也有同样的问题。我还没有亲自尝试过;我仍处于研究阶段,以了解如何更新我的应用以使用 Google Play 游戏服务。这就是我目前希望解决您面临的相同问题的方式(以及我偶然发现您的帖子的方式)。
    • 终于开始测试这个:它有效。 AccountName 是 gmail 地址,是的。
    【解决方案2】:

    我建议您查看/发布 logcat 跟踪。这是用户未登录时成功跟踪的样子:

    07-04 10:21:54.511: D/ian_(1781): MultiTab3 beginUserInitiatedSignIn
    07-04 10:21:54.531: D/ian_(1781): isGooglePlayServicesAvailable returned 0
    07-04 10:21:54.531: D/ian_(1781): beginUserInitiatedSignIn: continuing pending sign-in flow.
    07-04 10:21:54.611: D/ian_(1781): resolveConnectionResult: trying to resolve result: C      onnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{40f3ed38: android.os.BinderProxy@40ee3de0}}
    07-04 10:21:54.611: D/ian_(1781): result has resolution. Starting it.
    07-04 10:21:54.621: D/ian_(1781): startResolutionForResult - this may be prob ?
    07-04 10:23:29.480: D/ian_(1781): MultiPlayer onActivityResult called9001-1null
    07-04 10:23:29.520: D/ian_(1781): MultiPlayer passing onActivityResult to MultiTab3 Req/Resp/Data=9001-1null
    07-04 10:23:29.520: D/ian_(1781): MultiTab3 onActivityResult - passing through to GameHelper ...9001-1null
    07-04 10:23:29.520: D/ian_(1781): onActivityResult, req 9001 response -1
    07-04 10:23:29.520: D/ian_(1781): responseCode == RESULT_OK. So connecting.
    07-04 10:23:30.130: D/ian_(1781): onConnected: connected! client=1
    07-04 10:23:30.130: D/ian_(1781): All clients now connected. Sign-in successful.
    07-04 10:23:30.130: D/ian_(1781): All requested clients connected. Sign-in succeeded!
    

    beginUserInitiatedSignIn 仅在用户未登录(或出现问题)时启动 Google“选择帐户”对话框(结果已解决。启动它)。您会看到,在我的示例中,发生这种情况的原因是 SIGN_IN_REQUIRED。

    查看您的 logcat 并查看消息内容。如有必要,您可以获得更详细的跟踪。

    注意如果用户已登录,那么路径会更短、更简单,并且不会显示“选择帐户”对话框 - 或许最好先在其中一个示例应用上进行尝试?

    【讨论】:

    • 更新了我的 logcat。和你的基本一样。我不知道有什么办法可以拦截“startResolutionForResult”..
    • 我认为你最好弄清楚为什么你得到 mConnectionResult != null - 看起来你的登录无效。但是,如果您认为是,您可以尝试删除 resolveConnectionResult();
    • 好吧,我没有获得 Google+ 的授权,只有 Google 帐户,所以我对它失败并不感到惊讶。我只是想告诉 GamesClient 用户 X 是使用 Google 帐户 Y 进行身份验证的,因此请使用帐户 Y 也用于游戏服务授权 (Google+)。删除 reoslveConnectionResult() 将无济于事。
    • 嘿@IanB,这曾经对我有用,但就在几天前它停止工作,现在它抛出这个错误 com.google.android.gms.auth.GoogleAuthException: Unknown。知道为什么吗?我的瞄准镜有问题吗? gist.github.com/lawloretienne/7351151
    猜你喜欢
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2012-09-12
    • 2022-08-02
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2018-11-09
    相关资源
    最近更新 更多