Google 玩游戏

Google Play Games

大家好。

我正在尝试在我正在开发的游戏中实现成就。

我已经在 google play console 上设置了所有内容,获取了应用程序 ID,将以下内容放入清单中

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

并写了下面的方法

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

当我 运行 它时,我得到了我的帐户,但是当它 运行 时 Games.getPlayersClient(this, account); 我得到以下错误:

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

有人知道哪里出了问题吗?

提前致谢。

您是否在清单中正确添加了对 Google Play 服务的依赖?在 "common errors" 部分 here

"4. 在为 Android 开发时,将 Play 游戏 SDK 作为库项目包含在内,而不是作为独立的 JAR 确保 Google Play 服务 SDK 在您的 Android 项目中被引用为库项目,否则当您的应用无法找到 Google Play 服务资源时,这可能会导致错误。要了解如何设置 Android 项目以使用 Google Play 服务,请参阅 Setting Up Google Play Services。“

还有,你的清单里有吗?

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

您的 gradle 文件依赖项中有吗?

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

我认为你应该检查一下:

GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE).

如果该帐户没有权限,您应该使用

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

使用 startActivityForResult 接收具有 GAMES_LITE 范围的帐户。

GoogleSignIn.hasPermissions 对于空帐户也 returns false 这也可能是 getLastSignedInAccount 的结果。

示例:

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

我在显示排行榜时遇到了同样的问题,发现 Oleh 的解决方案有助于解决问题。请求正确的范围是关键。我在 onCreate 中构建 GoogleSignIn 客户端的代码是:

// 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);

稍后,在显示排行榜时,我是这样做的:

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

我的 Activity 也为 sign-in 进程实现了两个监听器:

public final class HomeActivity implements
    OnSuccessListener<Intent>,
    OnFailureListener

(来自包裹com.google.android.gms.tasks)

最后,我可以在 onSuccess 中显示排行榜

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

onFailure 在我的例子中只是向用户显示一个错误。但一定要有两个监听器,这样你在调试时就不会错过任何有用的细节。