1

我做了谷歌的API客户端使用此代码不能登录到Google Play游戏服务时,应用程序安装

GoogleSignInOptions options = new GoogleSignInOptions 
      .Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) 
      .requestServerAuthCode(SERVER_CLIENT_ID) 
      .build(); 

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

,并连接使用此代码服务

mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL); 

连接成功但我不能得到serverauthcode,因为 mGoogleApiClient.hasConnectedApi(Games.API)返回假 没有任何警告或错误

@Override 
public void onConnected(Bundle bundle) { 

    if (mGoogleApiClient.hasConnectedApi(Games.API)) { 
     Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
       new ResultCallback<GoogleSignInResult>() { 
        @Override 
        public void onResult(GoogleSignInResult googleSignInResult) { 

         // In this case, we are sure the result is a success. 
         GoogleSignInAccount signInAccount = googleSignInResult.getSignInAccount();       
         mAccountAuthCode = signInAccount.getServerAuthCode();  

         Player player = Games.Players.getCurrentPlayer(mGoogleApiClient); 
         mAccountId = player.getPlayerId(); 
         Log.i(MYTAG, "login success");   


        } 
       } 
     ); 
    } 
    else 
    {   
     Log.i(MYTAG, "connected but no Games.API Why!");    
    } 

奇怪的是,如果我只是断开并重新连接, mGoogleApiClient.hasConnectedApi(Games.API)返回true,所有的事情都很好。

此外,如果我刚刚重新启动应用程序 mGoogleApiClient.hasConnectedApi(Games.API)返回true,所有的事情都很好。

我想知道为什么我无法连接谷歌玩游戏服务API第一次尝试。

我使用的是最新的谷歌游戏服务SDK(10.2.4)

任何意见,将不胜感激

+0

尝试禁用即时运行。转到设置并取消选中启用即时运行,然后启动应用程序。 – Sallu

+0

Sallu //是即时运行Android Studio功能吗?我使用gradle直接构建我的apk并在控制台上使用adb进行部署。 – gguo

+0

是的。转至文件 - >设置 - >构建,执行,部署 - >即时运行 - >取消选中第一个复选框 – Sallu

回答

0

你可以参考这个thread。尝试重新启动您的设备。如果这没有帮助,请清除缓存。如果问题持续存在,您还可以尝试卸载并重新安装Play商店应用更新。

对于mGoogleApiClient.hasConnectedApi(Games.API)返回false,您可以检查此链接:GoogleApiClient.isConnected() return always false

你需要一对夫妇回调侦听器添加到Builder(或GoogleApiClient)。最有可能发生的情况是登录失败,因为您还没有权限。您需要处理该故障,以便用户可以授权您的应用程序。

更多阅读:http://www.androidpolice.com/2014/02/14/for-developers-google-play-services-4-2-includes-new-client-api-model-consolidates-connections-under-one-object/

希望这有助于!

+0

在我的情况下,mGoogleApiClient.isConnected()返回true。只有mGoogleApiClient.hasConnectedApi(Games.API)返回false。并且onConnectionFailedListener永远不会被调用。 – gguo

相关问题