【问题标题】:Android getServerAuthCode() without extra permissions没有额外权限的Android getServerAuthCode()
【发布时间】:2017-09-06 01:36:55
【问题描述】:

我正在尝试使用后端服务器对 Android 用户进行身份验证:

  • 应用调用getServerAuthCode()并使用HTTPS将授权码转发给我们的BE
  • BE 使用 GoogleAuthorizationCodeTokenRequest 将服务器授权代码交换为访问令牌
  • BE 将访问令牌传递给 www.googleapis.com/games/v1/applications,后者返回 playerId(这就是我真正需要的,我不关心电子邮件和其他用户信息)

这里概述了该过程:

这里

如果我使用 2017 年的指令,我可以在不请求任何额外权限的情况下获取 getServerAuthCode()。唯一的权限是:Google Play / 管理此游戏的游戏活动。这可以通过使用 play-service 10.2.1 指定 GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN 来实现。由于第三方依赖,我无法使用 10.2.1。

2016 年的文章解释了如何getServerAuthCode() 'old' 方式(使用 play-services 9.6.1),但如果不请求一些额外的权限,我就无法通过。

如果我这样做,我会收到“知道你在 google 上是谁”的询问:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestServerAuthCode(serverClientId)
    .requestScopes(Games.SCOPE_GAMES)
    .build();

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addConnectionCallbacks(this)
    .addOnConnectionFailedListener(this)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

        ...

protected void onActivityResult(int request, int response, Intent data) {
    super.onActivityResult(request, response, data);

    if (request == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();
            String authCode = acct.getServerAuthCode();
        }

如果我从 gso 中删除 .requestServerAuthCode(serverClientId),则 authCode 为空。

我尝试的另一件事(仅使用 Games.API 登录):

mGoogleApiClient = new GoogleApiClient.Builder(this, this, this)
    .addApi(Games.API)
    .build();

...

Games.GetServerAuthCodeResult result = Games.getGamesServerAuthCode(mGoogleApiClient, serverClinetId).await();

if (result.getStatus().isSuccess()) {
    String authCode = result.getCode();

我得到result.getStatus().isSuccess()=false 和result.getStatus().getStatusMessage() 返回STATUS_CLIENT_RECONNECT_REQUIRED (2)。在我看到的日志中:

[GetToken] GetToken failed with status code: NeedPermission
04-10 14:27:41.764: W/GamesServerAuthCodeOp(5775): Failed to retrieve the server auth code
04-10 14:27:41.764: W/GamesServerAuthCodeOp(5775): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission

最后,我能做到的最好的事情就是“仅”获得“查看您的基本个人资料信息”权限:

Scope scope = new Scope("https://www.googleapis.com/auth/userinfo.profile");

mGoogleApiClient = new GoogleApiClient.Builder(this, this, this)
    .addApi(Games.API)
    .addScope(scope)
    .build();

回顾一下 - 我想使用 9.6.1 获取服务器授权代码,而不提示任何额外权限(如 2017 年文章中使用 10.2.1 使用 DEFAULT_GAMES_SIGN_IN)。

我是否可以使用任何范围来为我提供服务器授权代码(以获取 playerId)而无需请求额外的权限?

【问题讨论】:

    标签: android permissions google-play-games scopes


    【解决方案1】:

    很遗憾,没有。您已经使用的范围是检索身份验证代码并同时获得playerId 的正确(也是最简单)的方法。

    【讨论】:

    • 谢谢 AL。 - 我还注意到,在 Google 的“我的帐户”/已连接的应用程序和网站中,旧应用程序都需要这些权限,但新应用程序不需要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2013-09-26
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 2019-01-21
    • 2017-09-10
    相关资源
    最近更新 更多