我尝试将GoogleApiClient
与Games.API
和Auth.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.API
与Auth.GOOGLE_SIGN_IN_API
一起使用?
我有什么选择连接Games.API
并获取用户档案信息?
答案 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...