Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API

懵懂的女人 提交于 2019-12-08 03:53:03

问题


I'm trying to connect GoogleApiClient with Games.API and Auth.GOOGLE_SIGN_IN_API (for getting User Profile Information).
Getting this Error:

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

Code:

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();

Why I can't use Games.API with Auth.GOOGLE_SIGN_IN_API?
What alternative I have to connect Games.API and get User Profile Information?


回答1:


Resolved by my self.
No need to add Auth.GOOGLE_SIGN_IN_API for getting User Profile Information if you already connected Games.API.


Connect:

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

If failed:

@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;
    }
}

If connected:
Get User Profile Information:

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


来源:https://stackoverflow.com/questions/37317695/auth-google-sign-in-api-cannot-be-used-with-games-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!