【问题标题】:Google Play Games谷歌玩游戏
【发布时间】:2017-12-01 13:11:07
【问题描述】:

大家好。

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

我已经在 google play 控制台上设置了所有内容,获取了 app-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:游戏 API 需要 https://www.googleapis.com/auth/games_lite函数。

任何人都知道可能出了什么问题吗?

提前致谢。

【问题讨论】:

  • 这与 Unity 有关吗?如果是,那么您可以检查此github forum
  • 嗨@noogui。没有。它与 Unity 无关。
  • 请注意,requestEmail() 将阻止自动登录发生,用户需要查看登录 UI。

标签: java android google-play google-play-games illegalstateexception


【解决方案1】:

我认为你应该检查一下:

GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE).

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

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

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

GoogleSignIn.hasPermissions 也会为空帐户返回 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();
            }
          });
}

【讨论】:

    【解决方案2】:

    您是否在清单中正确添加了对 Google Play 服务的依赖项?在“常见错误”部分here

    "4. 为 Android 开发时,将 Play Games 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}"

    【讨论】:

    • 我遵循了所有指南。许多人。尝试了很多解决方案。如果我使用 DEFAULT_SIGN_IN 而不是 DEFAULT_GAMES_SIGN_IN 我不会收到任何错误,但它不会返回玩家数据。
    【解决方案3】:

    我在显示排行榜时遇到了同样的问题,发现 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 还实现了两个侦听器以使登录过程正常工作:

    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 在我的例子中只是向用户显示一个错误。但请确保同时拥有两个侦听器,以免在调试时错过任何有用的细节。

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多