繁体   English   中英

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

[英]Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API

我正在尝试将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并获取用户个人资料信息?

由我自己解决。
如果您已经连接了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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM