Search code examples
androidgoogle-play-gamesgoogle-api-clientgoogle-signin

Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API


I'm trying to connect GoogleApiClient with Games.API and Auth.GOOGLE_SIGN_IN_API (for getting User Profile Information).
Getting this Error:

java.lang.IllegalStateException: Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API

Code:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestProfile()
                .build();

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

Why I can't use Games.API with Auth.GOOGLE_SIGN_IN_API?
What alternative I have to connect Games.API and get User Profile Information?


Solution

  • Resolved by my self.
    No need to add Auth.GOOGLE_SIGN_IN_API for getting User Profile Information if you already connected Games.API.


    Connect:

    googleApiClient = new GoogleApiClient.Builder(activity)
                    .addConnectionCallbacks(connectionCallbacks)
                    .addOnConnectionFailedListener(onConnectionFailedListener)
                    .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                    .build();
    

    If failed:

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this, REQUEST_RESOLVE_ERR);
            } catch (IntentSender.SendIntentException e) {
                googleApiClient.connect();
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_RESOLVE_ERR:
                if (resultCode == RESULT_OK) {
                    if (!googleApiClient.isConnecting()
                                && !googleApiClient.isConnected())
                        googleApiClient.connect();
                }
                break;
        }
    }
    

    If connected:
    Get User Profile Information:

    String name = Games.Players.getCurrentPlayer(googleApiClient).getDisplayName();
    Uri photo = Games.Players.getCurrentPlayer(googleApiClient).getIconImageUri();
    //and more...