Google Play游戏

今天是个好日子。

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

我已经在google play console上设置了所有内容,获得了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_litefunction。

任何人都知道什么可能是错的?

提前致谢。

我想你应该检查一下:

 GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE). 

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

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

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

对于null帐户, 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(); } }); } 

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

“4.开发Android时,将Play Games SDK作为库项目,而不是独立的JAR确保将Google Play服务SDK作为Android项目中的库项目引用,否则可能会导致错误应用程序无法找到Google Play服务资源。要了解如何设置Android项目以使用Google Play服务,请参阅设置Google Play服务 。

另外,你的清单中有吗?

你有gradle文件依赖项吗?

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

在展示排行榜时我遇到了同样的问题,发现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); } 

我的活动还实现了两个用于登录过程的侦听器:

 public final class HomeActivity implements OnSuccessListener, OnFailureListener 

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

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

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

在我的情况下,onFailure只会向用户显示错误。 但是一定要有两个监听器,这样在调试时就不会遗漏任何有用的细节。

Interesting Posts