Google Games Api的正確接入方式

最近項(xiàng)目需要接入Google Games登錄,按照Google官方建議,我們選擇了Google Service SDK V10.2.0版本。但在接入Google Games Api時(shí),卻發(fā)生了些不愉快的事情。

Google的官方教程

Google Api 初始化

// Defaults to Games Lite scope, no server component
  GoogleSignInOptions gso = new
     GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build();

// OR for apps with a server component
   GoogleSignInOptions gso = new
     GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
         .requestServerAuthCode(SERVER_CLIENT_ID)
         .build();

// OR for developers who need real user Identity
  GoogleSignInOptions gso = new
     GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
         .requestEmail()
         .build();

// Build the api client.
     mApiClient = new GoogleApiClient.Builder(this)
                .addApi(Games.API)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .addConnectionCallbacks(this)
                .build();
    }

Google Api 連接

mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);

Google登錄成功后的邏輯

 @Override
    public void onConnected(Bundle connectionHint) {
         if (mGoogleApiClient.hasConnectedApi(Games.API)) {
            Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
                    new ResultCallback<GoogleSignInResult>() {
                        @Override
                        public void onResult(
                                @NonNull GoogleSignInResult googleSignInResult) {
                            if (googleSignInResult.isSuccess()) {
                                onSignedIn(googleSignInResult.getSignInAccount(),
                                        connectionHint);
                            } else {
                                Log.e(TAG, "Error with silentSignIn: " +
                                        googleSignInResult.getStatus());
                                // Don't show a message here, this only happens
                                // when the user can be authenticated, but needs
                                // to accept consent requests.
                                handleSignOut();
                            }
                        }
                    }
            );
        } else {
            handleSignOut();
        }
    }

Google提供的方法很簡(jiǎn)單直接,但是這段實(shí)例代碼存在的問(wèn)題是:

mApiClient.hasConnectedApi(Games.API)

一直返回false?。?!

最初的解決方案:

通過(guò)打印日志,我發(fā)現(xiàn),雖然此時(shí)mApiClient.hasConnectedApi(Games.API)返回false,但是
mApiClient.hasConnectedApi(Auth.GoogleSignInApi)返回的是true。

所以最開(kāi)始時(shí),我將上面的條件改成了mApiClient.hasConnectedApi(Auth.GoogleSignInApi),但是這個(gè)問(wèn)題又來(lái)了,獲取此時(shí)獲取ServerAuthCode失敗了,原因是:[SIGN_IN_REQUIRED]

看到這個(gè)狀態(tài),我突然想到了一個(gè)新的解決方案。

最終的解決方案:

由于這個(gè)SIGN_IN_REQUIRED錯(cuò)誤碼的啟示,我發(fā)現(xiàn)可以將onConnected函數(shù)中代碼改成這樣:

@Override
    public void onConnected(@Nullable Bundle bundle) {
        ConnectionResult result = mApiClient.getConnectionResult(Games.API);
        if (!result.isSuccess()) {
            onConnectionFailed(result);
        } else {
            //TODO 做你想要做的事情
        }
}

一些可能的疑惑

看到這里,有人會(huì)建議,為什么不在第一個(gè)解決方法中甚至一開(kāi)始時(shí)就使用google提供的這個(gè)方法登錄呢?

private void handleSignin() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

為什么不呢?

問(wèn)題很簡(jiǎn)單:
就是每次調(diào)用這個(gè)方法,都會(huì)彈出一個(gè)半透明的Activity,感覺(jué)很詭異。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,351評(píng)論 25 708
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評(píng)論 19 139
  • 22年12月更新:個(gè)人網(wǎng)站關(guān)停,如果仍舊對(duì)舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,438評(píng)論 22 257
  • 文/鐵爐 負(fù)能量是一...
    湘邵鐵爐閱讀 1,607評(píng)論 11 9
  • 前幾天,有一位讀者給我發(fā)郵件,大意是說(shuō)很喜歡那篇《恐懼是一只猙獰的膽小鬼》。她會(huì)經(jīng)常拿出來(lái)翻閱,用來(lái)對(duì)抗自己的恐懼...
    彼得貓烏金閱讀 639評(píng)論 0 28

友情鏈接更多精彩內(nèi)容