2017-08-24 65 views
2

我有一個使用Google帳戶登錄的工作應用程序。GoogleApiClient:Games.API和Auth.GOOGLE_SIGN_IN_API不能一起工作

我建立我的客戶這樣

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN) 
     .requestEmail() 
     .requestProfile() 
     .requestIdToken(getString(R.string.server_client_id)) 
     .build(); 

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .addConnectionCallbacks(this) 
       .build(); 

現在我決定增加導致以下客戶端的Games.API

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

然而,當我嘗試連接客戶端

googleApiClient.connect(); 

我收到以下例外情況:

java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead. 

所以我讀了異常,並修改了我的聯繫電話,以

googleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL); 

所以現在我沒有得到任何更多的例外情況,但客戶端永遠不會被連接出於某種原因。任何想法爲什麼客戶端現在不連接?

編輯:完整的工作代碼示例

package at.hakkon.space.signin; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.Toast; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.games.Games; 
import com.google.example.games.basegameutils.BaseGameUtils; 

import at.hakkon.space.R; 
import at.hakkon.space.activity.MainActivity; 
import at.hakkon.space.application.ApplicationClass; 



public class GoogleSignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks { 

    private static final String TAG = "T1_GSignInActivity"; 

    private static int RC_SIGN_IN = 9001; 

    private boolean mResolvingConnectionFailure = false; 
    private boolean mAutoStartSignInflow = true; 
    private boolean mSignInClicked = false; 

    private static GoogleApiClient googleApiClient; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG, "GoogleSignInActivity"); 

     setupGoogleClient(); 
    } 



    public void setupGoogleClient() { 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .build(); 

     ApplicationClass.getInstance().setGoogleClient(googleApiClient); 
    } 


    @Override 
    public void onStart() { 
     super.onStart(); 
     googleApiClient.connect(); 
    } 


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

     if (requestCode == RC_SIGN_IN) { 
      mSignInClicked = false; 
      mResolvingConnectionFailure = false; 
      if (resultCode == RESULT_OK) { 
       googleApiClient.connect(); 
      } else { 
       // Bring up an error dialog to alert the user that sign-in 
       // failed. The R.string.signin_failure should reference an error 
       // string in your strings.xml file that tells the user they 
       // could not be signed in, such as "Unable to sign in." 
       BaseGameUtils.showActivityResultError(this, 
         requestCode, resultCode, R.string.sign_in_other_error); 
      } 
     } 
    } 

    private void startNextActivity() { 
     finish(); 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     startActivity(intent); 
    } 


    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Toast.makeText(this, "Google Client Connection failed:\n" + connectionResult.toString(), Toast.LENGTH_LONG).show(); 

     if (mResolvingConnectionFailure) { 
      // already resolving 
      return; 
     } 

     // if the sign-in button was clicked or if auto sign-in is enabled, 
     // launch the sign-in flow 
     if (mSignInClicked || mAutoStartSignInflow) { 
      mAutoStartSignInflow = false; 
      mSignInClicked = false; 
      mResolvingConnectionFailure = true; 

      // Attempt to resolve the connection failure using BaseGameUtils. 
      // The R.string.signin_other_error value should reference a generic 
      // error string in your strings.xml file, such as "There was 
      // an issue with sign-in, please try again later." 

      if (!BaseGameUtils.resolveConnectionFailure(this, googleApiClient, connectionResult, RC_SIGN_IN, R.string.sign_in_other_error)) { 
       mResolvingConnectionFailure = false; 
       Toast.makeText(this, "Conflict NOT RESOLVED :((", Toast.LENGTH_LONG).show(); 
      }else{ 
       Toast.makeText(this, "Conflict RESOLVED", Toast.LENGTH_LONG).show(); 
       //startNextActivity(); 
      } 

     } 

     // Put code here to display the sign-in button 

     //Log.d(TAG, "onConnectionFailed:" + connectionResult); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     Toast.makeText(this, "Google Client Connected.", Toast.LENGTH_LONG).show(); 
     startNextActivity(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     Toast.makeText(this, "Google Client Connection Suspended.", Toast.LENGTH_LONG).show(); 
    } 
} 

回答

1

使用

mGoogleApiClient = new GoogleApiClient.Builder(activity) 
      .addApi(Games.API) 
      .addConnectionCallbacks(callbacks) 
      .build(); 

然後

mGoogleApiClient.connect(); 

如果您在控制檯正確配置你的遊戲 - 它會自動連接並獲取訪問遊戲功能(成就和排行榜f或示例)

class GoogleConnectionCallbacks implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks { 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     MyLogger.d("DBG_GGL_LDB", "connected"); 
     Player p = Games.Players.getCurrentPlayer(mGoogleApiClient); 
     String displayName; 
     if (p == null) { 
      Log.w("DBG_GGL_LDB", "mGamesClient.getCurrentPlayer() is NULL!"); 
      displayName = "???"; 
     } else { 
      displayName = p.getDisplayName(); 
     } 
     MyLogger.d("DBG_GGL_LDB", "Hello, " + displayName); 

     if (mGoogleApiClient.hasConnectedApi(Games.API)) { 

      MyLogger.d("DBG_GGL_LDB", "hasConnectedApi true - can user leaderboard"); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
     MyLogger.d("DBG_GGL_LDB", "connection suspended"); 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
     MyLogger.d("DBG_GGL_LDB", "connection failed " + connectionResult.getErrorMessage()); 

    } 
} 

我已經面臨這個問題很長,這是所有使用.addApi(Auth.GOOGLE_SIGN_IN_API, gso)

+0

嘿男人。感謝您的回覆,但我發佈的代碼實際上工作得很好。我非常專注於測試設備,所以我沒有測試任何其他設備。結果發現其他設備的工作,並在我重新安裝谷歌播放它的測試服務也起作用。 – Markus

0

,因爲代碼工作完全正常比我的測試設備的任何其他設備上。在我的測試設備上重新安裝Google Play之後,它也可以運行!我不知道是什麼導致了問題,但重新安裝它。

原來我根本不需要SIGN_IN。看看Alexanders的答案,如果你需要的話!

+1

你已經刪除了SIGN_IN api,這就是爲什麼它開始工作,我認爲,不是因爲重新安裝 –

+0

你是對的,它已經2個月了,所以細節是模糊的。原來我不需要SignIn了。不過,我必須重新安裝谷歌播放出於某種原因才能正常工作。 – Markus