简体   繁体   中英

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.

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

Have you added a dependency on Google Play Services in your manifest correctly? In the "common errors" section here

"4. When developing for Android, include the Play Games SDK as a library project, not as a standalone JAR Make sure that the Google Play services SDK is referenced as a library project in your Android project, otherwise this could lead to errors when your app is unable to find Google Play services resources. To learn how to set up your Android project to use Google Play services, see Setting Up Google Play Services ."

Also, do you have in your manifest?

<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

Do you have in your gradle file dependencies?

compile "com.google.android.gms:play-services-games:${gms_library_version}" compile "com.google.android.gms:play-services-auth:${gms_library_version}"

I had the same issue when showing a Leaderboard and found that Oleh's solution helped solve the issue. Requesting the right Scope is key. My code to build the GoogleSignIn client in onCreate is:

// Build a GoogleSignInClient with the options specified by gso.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(clientId)
            .requestScopes(Games.SCOPE_GAMES_LITE)
            .build();
mGoogleSignInClient = GoogleSignIn.getClient(HomeActivity.this, gso);

Later, when showing the Leaderboard, I do it like so:

GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (null != account) {
    // check permissions, show Leaderboard when allowed to
    boolean hasGamesLitePermissions = GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE);
    if (hasGamesLitePermissions) {
        Games.getLeaderboardsClient(this, account)
            .getAllLeaderboardsIntent()
            .addOnSuccessListener(this)
            .addOnFailureListener(this);
}

My Activity does also implement two listeners for the sign-in process to work:

public final class HomeActivity implements
    OnSuccessListener<Intent>,
    OnFailureListener

(from the package com.google.android.gms.tasks)

And finally, in onSuccess I can show the Leaderboard

public void onSuccess(Intent intent) {
    startActivityForResult(intent, RC_LEADERBOARD_UI);
}

onFailure just displays an error to the user in my case. But be sure to have both listeners so you don't miss any useful details when debugging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM