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

时间:2016-05-19 08:21:17

标签: 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并获取用户档案信息?

1 个答案:

答案 0 :(得分:6)

由我自己解决。
如果您已连接Auth.GOOGLE_SIGN_IN_API,则无需添加Games.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...