javaandroidgoogle-playgoogle-play-gamesillegalstateexception

Google Play Games


I'm trying to implement Achievements in a game I'm developing.

I already set everything on google play console, got the app-id, put in the manifest the following

    <meta-data
        android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />

and wrote the following method

        GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
        int temp = googleApiAvailability.isGooglePlayServicesAvailable(this);
        if ( temp != 0)
            return;

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
                .requestEmail()
                .build();

        GoogleSignIn.getClient(this, gso);

        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
        PlayersClient player = Games.getPlayersClient(this, account);

When I run it I get my account, but as it runs Games.getPlayersClient(this, account); I get the following error:

java.lang.IllegalStateException: Games APIs requires https://www.googleapis.com/auth/games_lite function.

Anybody as any idea what could be wrong?


Solution

  • I think you should check:

    GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE).
    

    And if there is no permissions in that account you should use

    GoogleSignIn.getClient(this, gso).silentSignIn or GoogleSignIn.getClient(this, gso).getSignInIntent() 
    

    with startActivityForResult to receive account with GAMES_LITE scope.

    GoogleSignIn.hasPermissions also returns false for null account which could be also result of the getLastSignedInAccount.

    Example:

    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    
    if (GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE)) {
        onSignIn(account);
    } else {
        signInClient
          .silentSignIn()
          .addOnCompleteListener(
              this,
              task -> {
                if (task.isSuccessful()) {
                  onSignIn(task.getResult());
                } else {
                  resetSignedIn();
                }
              });
    }