2017-02-21 43 views
0

我已經使用Firebase與Google進行了登錄活動。用戶可以毫無問題登錄現在我想不過來顯示在另一個活動排行榜,當我檢查,如果用戶登錄,我儘量表現排行榜我得到的錯誤:Android FireBase - 連接到遊戲服務排行榜

E/UncaughtException: java.lang.IllegalStateException: GoogleApiClient must be connected.

我如何連接GoogleApiClient使用firebase?我曾嘗試使用mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);,但這也不起作用。

這裏我的代碼:

 protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_achievements); 


       // Configure Google Sign In 
       GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
         .requestIdToken(getString(R.string.default_web_client_id)) 
         .requestEmail() 
         .build(); 

       mGoogleApiClient = new GoogleApiClient.Builder(this) 
         .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() { 
          @Override 
          public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
           // connection failed, should be handled 
          } 
         }) 
         .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
         .build(); 

       mAuth = FirebaseAuth.getInstance(); 

       mAuthListener = new FirebaseAuth.AuthStateListener() { 
        @Override 
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { 
         FirebaseUser user = firebaseAuth.getCurrentUser(); 
         if (user != null) { 
          // User is signed in 
          Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid()); 

    ////// is crashing googleapiclient not connected 

startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient, 
            getString(R.string.leaderboard_la_classifica)), REQUEST_LEADERBOARD); 

         } else { 
          // User is signed out 
         } 
         // ... 
        } 
       }; 

      } 

回答

0

詳情請參閱https://firebase.google.com/docs/auth/android/google-signin

構建客戶端時,您需要使用GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_firebase_games_signin); 

    String webclientId = getString(R.string.web_client_id); 

    GoogleSignInOptions options = 
      new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) 
        .requestServerAuthCode(webclientId) 
        .requestEmail() 
        .requestIdToken() 
        .build(); 

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

} 

然後開始明確登錄開始簽到意圖:

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

你也應該使用自動管理客戶端,或者乾脆叫onStop()mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);onStart()斷開。

在onActivityResult,處理的結果明確登錄意圖:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...); 
    if (requestCode == RC_SIGN_IN) { 
     GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
     if (result.isSuccess()) { 
      // Google Sign In was successful, authenticate with Firebase 
      GoogleSignInAccount acct = result.getSignInAccount(); 

      completeFirebaseAuth(acct); 

      // At this point, the Games API can be called. 

     } else { 
      // Google Sign In failed, update UI appropriately 
      // ... 
     } 
    } 
} 

完成火力地堡認證:

private void completeFirebaseAuth(GoogleSignInAccount acct) { 
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null); 
    FirebaseAuth mAuth = FirebaseAuth.getInstance(); 
    mAuth.signInWithCredential(credential) 
      .addOnCompleteListener(this, new 
        OnCompleteListener<AuthResult>() { 
       @Override 
       public void onComplete(@NonNull Task<AuthResult> task) { 
        Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); 

        // If sign in fails, display a message to the user. If sign in succeeds 
        // the auth state listener will be notified and logic to handle the 
        // signed in user can be handled in the listener. 
        if (!task.isSuccessful()) { 
         Log.w(TAG, "signInWithCredential", task.getException()); 
        } 
        // ... 
       } 
      }); 
} 

你需要處理的另一種情況是沉默登錄,這在恢復應用程序時發生:

@Override 
public void onConnected(@Nullable Bundle bundle) { 

    // Handle the silent sign-in case. 

    if (mGoogleApiClient.hasConnectedApi(Games.API)) { 
     Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
       new ResultCallback<GoogleSignInResult>() { 
        @Override 
        public void onResult(
          @NonNull GoogleSignInResult googleSignInResult) { 
         if (googleSignInResult.isSuccess()) { 
          completePlayGamesAuth(
            googleSignInResult.getSignInAccount()); 
         } else { 
          Log.e(TAG, "Error with silentSignIn: " + 
            googleSignInResult.getStatus()); 
         } 
        } 
       } 
     ); 
    } 
}