我应该使用GoogleAuthUtil

我应该使用GoogleAuthUtil

本文介绍了我应该使用GoogleAuthUtil.getToken(...)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的服务器后端进行身份验证,因此我知道客户端是真实的。 在我的Android游戏中,我通过 GoogleApiClient 连接到 Games.API



我只希望通过 Games.API 登录一次,因为这给了我很多优势(Google Play游戏排行榜,成就等等)

我可以使用 GoogleAuthUtil.getToken(...)来获得授权令牌,我可以在登录到 Games.API 之后执行此操作,这似乎给了我一个令牌。目前为止还不错。



但,并说我应该。但据我了解,这种方法需要我使用

  Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
startActivityForResult(signInIntent,RC_SIGN_IN);

这意味着为游戏发起附加登录 .API 即可。此外, addApi 都不可能是 Games.API Auth.GOOGLE_SIGN_IN_API 改为相同的 GoogleApiClient



好的,最新的谷歌播放服务(至少r29),使用我可以使用 Games.API Games.getGamesServerAuthCode(...)来获取我的服务器的认证令牌。但是这有两个问题:(1)它要求Android 6.0或更高版本阻止80%的市场,并且(2)它已被弃用!



问题: 我应该使用 GoogleAuthUtil.getToken (...)或不是,如果不是,我只能使用 Games.API



通过登录我的意思是向用户提供登录视觉效果。我不介意登录别的东西,只要用户不必与登录进行交互...






我最初问过,当我第一次出发。首先,我应该使用<$ c $> C> GoogleAuthUtil.getToken(...)。它已被弃用;



为了达到我想要的效果,我发现以下作品非常完美......是否是我不知道的最佳方式。



首先,使用 Auth.GOOGLE_SIGN_IN 登录:

  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(
GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(YOUR-SERVER-CLIENT-ID)
.build ();

mGoogleApiClientForSignIn =新的GoogleApiClient.Builder(mActivity,this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();

mGoogleApiClientForSignIn.connect();

成功后,最终会调用 onConnected(...)您可以在其中协商第二次登录到Games.API。这必须在新的 GoogleApiClient 上单独执行,因为您不能混合使用 Games.API Auth.GOOGLE_SIGN_IN

  mGoogleApiClientForGames = new GoogleApiClient.Builder(mActivity,this,this) 
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER)
.build();

mGoogleApiClientForGames.connect();

根据新的 2016年更新, GoogleSignIn 每场比赛只出现一次(甚至在两台设备之间!),之后用户不会看到 GoogleSignIn 的任何可视化日志。唯一的视觉登录将是保存的游戏快照选择屏幕。



这适用于Android 2.3(使用google-play-services ),且不含弃用警告。 Huzzah!

Background:

I need to authenticate on my server back-end so I know the client is genuine. In my Android game I connect to Games.API via GoogleApiClient.

I only want to have to sign in once, which I want to do via Games.API, as this gives me many advantages (Google Play Games leaderboards, achievements, etc.)

I have been able to get an authorisation token using GoogleAuthUtil.getToken(...) which I can do after I sign into Games.API, which seems to give me a token. Good so far.

But Google says this is not safe and says I should migrate to ID token flow instead. But, as I understand it this approach would require me to use

Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);

which means instigating an additional sign in to that for Games.API. Furthermore, it is not possible to addApi both Games.API and Auth.GOOGLE_SIGN_IN_API to the same GoogleApiClient !

Ok, so upgrade to the latest google-play-services (at least r29), using which I can use Games.API with Games.getGamesServerAuthCode(...) to obtain an auth token for my server. But this has two problems: (1) it requires Android 6.0 or above which blocks out 80% of the market, and (2) it's deprecated !


Question:

Should I use GoogleAuthUtil.getToken(...) or not, and if not what are my options given that I only want to sign in using Games.API ?

By sign in I mean present the user with log in visuals. I don't mind signing into something else so long as the user does not have to interact with the sign in...


Note:

I originally asked this question when I first started out. The current question hopefully clarifies the situation.

解决方案

Firstly, I should not use GoogleAuthUtil.getToken(...). It's deprecated; end of.

To achieve what I want I found the following works perfectly... whether it's the best way I have no idea.

First, sign in using Auth.GOOGLE_SIGN_IN:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(
                                          GoogleSignInOptions.DEFAULT_SIGN_IN)
    .requestEmail()
    .requestIdToken("YOUR-SERVER-CLIENT-ID")
    .build();

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

mGoogleApiClientForSignIn.connect();

On success this will eventually call onConnected(...) from where you can negotiate a second sign in to Games.API. This has to be performed separately on a new GoogleApiClient because you can't mix Games.API and Auth.GOOGLE_SIGN_IN:

mGoogleApiClientForGames = new GoogleApiClient.Builder(mActivity, this, this)
    .addApi(Games.API).addScope(Games.SCOPE_GAMES)
    .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER)
    .build();

mGoogleApiClientForGames.connect();

As per the new Play Games Permissions update for 2016, the GoogleSignIn only appears once per game (even between devices !), after which the user is not presented with any visual log in screens for GoogleSignIn. The only visual login will be the Saved Games snapshot selection screen.

This works with Android 2.3 (use google-play-services r28) and without deprecation warnings. Huzzah !

这篇关于我应该使用GoogleAuthUtil.getToken(...)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 04:04