我正在尝试使用后端服务器对 Android 用户进行身份验证:
- 应用程序调用
getServerAuthCode()
并使用 HTTPS 将授权码转发给我们的 BE - BE 使用 GoogleAuthorizationCodeTokenRequest 为访问 token 交换服务器授权码
- BE 将访问 token 传递给 www.googleapis.com/games/v1/applications,它返回 playerId(这就是我真正需要的,我不关心电子邮件和其他用户信息)
程序概述如下:
- https://android-developers.googleblog.com/2016/01/play-games-permissions-are-changing-in.html (2016)
这里
如果我使用 2017 年的指令,我可以在不请求任何额外权限的情况下使用 getServerAuthCode()。唯一的权限是:Google Play/管理您对此游戏的游戏 Activity 。这可以通过指定 GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN
来实现,它可以通过使用 play-service 10.2.1 获得。由于第 3 方依赖性,我无法使用 10.2.1。
2016 年的文章解释了如何getServerAuthCode()
“旧”方式(使用播放服务 9.6.1),但我无法理解无需请求一些额外的权限。
如果我这样做,我会被要求“知道你在谷歌上是谁”:
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)?
最佳答案
不幸的是,没有。您已经使用的范围是检索授权代码并同时获取 playerId
的正确(也是最简单)的方法。
关于没有额外权限的Android getServerAuthCode(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43332825/