Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

java - Google Play Games

Good day everyone.

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?

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
            }
          });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...