我正在尝试将 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
并获取用户配置文件信息?
最佳答案
我自己解决的。
如果您已经连接 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/