GoogleApiClient第一次连接总是失败,但第二次就成功了

9

我为应用程序设置了 Google Plus 登录,使用 GoogleApiClient 进行连接。

每当第一次安装应用程序并尝试通过 GoogleApiClient 进行连接时,它永远无法成功,总是最终在 onConnectionFailed 处结束,其中 result 包含:

ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4130e760: android.os.BinderProxy@4130e700}}

但是当第二次登录时,它被称为并且成功连接,onConnected也会被触发。为什么会这样?有没有可能在第一次尝试时就成功连接?

我的Builder参数有问题吗?

public void connectGoogleApi() {
    mGoogleApiClient = new GoogleApiClient.Builder(mainAppContext).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN).build();
    mGoogleApiClient.connect();
}

public void onConnectionFailed(ConnectionResult result) {
    if (!result.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
        return;
    }

    if (!mIntentInProgress) {
        // Store the ConnectionResult for later usage
        mConnectionResult = result;
        resolveSignInError();
    }

}

1
很明显,根据错误提示,用户需要允许您的应用程序进行身份验证。 - tyczj
没有对话框弹出,下一次如何成功地完成操作而无需用户做任何事情? - user971741
2个回答

2
官方文档如下所述:这里

如果您正在使用GoogleApiClient连接需要身份验证的API(例如Google Drive或Google Play Games),您的第一次连接尝试很可能会失败,并且您的应用程序将接收到onConnectionFailed()调用,其中包含SIGN_IN_REQUIRED错误,因为未指定用户帐户。


太好了,知道了。那帮了我很多! - Hussein El Feky

1
我遇到了同样的问题,再次调用“connect()”,这次在“onConnected”方法内解决了它。奇怪。
@Override
   public void onConnected(final Bundle arg0) {
   Logger.log("On connected");
   DevicePreferences.getGoogleApiClient().connect();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接