我使用此代码制作了google-api-client
GoogleSignInOptions options = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestServerAuthCode(SERVER_CLIENT_ID)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, options)
.addApi(Games.API)
.build();
并使用此代码连接到服务
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
连接成功,但我无法获得serverauthcode,因为 mGoogleApiClient.hasConnectedApi(Games.API)返回false 没有任何警告或错误
@Override
public void onConnected(Bundle bundle) {
if (mGoogleApiClient.hasConnectedApi(Games.API)) {
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient).setResultCallback(
new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
// In this case, we are sure the result is a success.
GoogleSignInAccount signInAccount = googleSignInResult.getSignInAccount();
mAccountAuthCode = signInAccount.getServerAuthCode();
Player player = Games.Players.getCurrentPlayer(mGoogleApiClient);
mAccountId = player.getPlayerId();
Log.i(MYTAG, "login success");
}
}
);
}
else
{
Log.i(MYTAG, "connected but no Games.API Why!");
}
奇怪的是,如果我只是断开并重新连接, mGoogleApiClient.hasConnectedApi(Games.API)返回true,一切正常。
此外,如果我只是重新启动应用程序 mGoogleApiClient.hasConnectedApi(Games.API)返回true,一切正常。
我想知道为什么我不能在第一次尝试时连接google-play-games-service api。
我正在使用最新的Google Play服务sdk(10.2.4)
任何建议都将不胜感激
答案 0 :(得分:0)
您可以参考此thread。尝试重启设备。如果这没有帮助,那么清除缓存。如果问题仍然存在,您还可以尝试卸载并重新安装Play商店应用更新。
对于 mGoogleApiClient.hasConnectedApi(Games.API)返回false ,您可以查看以下链接:GoogleApiClient.isConnected() return always false
您需要向Builder(或GoogleApiClient)添加几个回调侦听器。最可能发生的是登录失败,因为您还没有权限。您需要处理该失败,以便用户可以授权您的应用。
希望这有帮助!