2015-01-14 112 views
7

我有一个谷歌加登录通过GoogleApiClient设置为应用程序。GoogleApiClient连接第一次总是失败,但第二次获得成功

只要安装该应用程序第一次尝试,使通过GoogleApiClient方面,它永远不会成功,并且总是在onConnectionFailed结束了result包含:

ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{4130e760: [email protected]}} 

但是当第二次登录其称之为获得成功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(); 
    } 

} 
+0

是不是很明显的错误,用户需要允许您的应用程序进行身份验证 – tyczj

+0

没有对话框出现,以及如何在没有用户做任何事情时获得成功? – Maven

回答

0

我有同样的问题,称“接()”,这一次里面“onConnected”的方法固定它。奇怪。

@Override 
    public void onConnected(final Bundle arg0) { 
    Logger.log("On connected"); 
    DevicePreferences.getGoogleApiClient().connect(); 
} 
2

官方文档说here

如果使用GoogleApiClient连接到需要身份验证,如谷歌驱动器或谷歌玩游戏的API,有一个很好的机会,你的第一个连接尝试将失败并且您的应用将因SIGN_IN_REQUIRED错误接收到对onConnectionFailed()的调用,因为未指定用户帐户。

+0

很高兴知道。这帮了我很多! –

相关问题