android - Auth.GOOGLE_SIGN_IN_API 不能与 Games.API 一起使用

标签 android google-play-games google-api-client google-signin

我正在尝试将 GoogleApiClientGames.APIAuth.GOOGLE_SIGN_IN_API 连接(用于获取用户配置文件信息)。
收到此错误:

java.lang.IllegalStateException: Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API

代码:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestProfile()
                .build();

googleApiClient = new GoogleApiClient.Builder(activity)
                .addConnectionCallbacks(connectionCallbacks)
                .addOnConnectionFailedListener(onConnectionFailedListener)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

为什么我不能将 Games.APIAuth.GOOGLE_SIGN_IN_API 一起使用?
我有什么替代方法来连接 Games.API 并获取用户配置文件信息?

最佳答案

我自己解决的。
如果您已经连接 Games.API,则无需添加 Auth.GOOGLE_SIGN_IN_API 来获取用户个人资料信息。


连接:

googleApiClient = new GoogleApiClient.Builder(activity)
                .addConnectionCallbacks(connectionCallbacks)
                .addOnConnectionFailedListener(onConnectionFailedListener)
                .addApi(Games.API).addScope(Games.SCOPE_GAMES)
                .build();

如果失败:

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    if (connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, REQUEST_RESOLVE_ERR);
        } catch (IntentSender.SendIntentException e) {
            googleApiClient.connect();
            e.printStackTrace();
        }
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case REQUEST_RESOLVE_ERR:
            if (resultCode == RESULT_OK) {
                if (!googleApiClient.isConnecting()
                            && !googleApiClient.isConnected())
                    googleApiClient.connect();
            }
            break;
    }
}

如果连接:
获取用户个人资料信息:

String name = Games.Players.getCurrentPlayer(googleApiClient).getDisplayName();
Uri photo = Games.Players.getCurrentPlayer(googleApiClient).getIconImageUri();
//and more...

关于android - Auth.GOOGLE_SIGN_IN_API 不能与 Games.API 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37317695/

相关文章:

android - Binder 线程失败

java - 为什么我的 Java 字符串被填充为空值?

java - Google Play 游戏服务中基于套接字与不可靠的消息传递

java - 无法识别 HttpTransport 类型的 create RequestFactory() 方法

android - 自定义进度对话框

Android,如何在启动其他 Activity 时保持 CountDownTimer 运行

java - 适用于 Java 的 AndroidPublisher (V3) Google API 客户端库示例

java - 配置您的 OAuth 客户端? (谷歌表格 API)

android - Google Play 游戏服务 BaseGameUtils 错误

android - Google Play 游戏初始化时如何隐藏 Android 标题栏?