2015-05-16 188 views
0

我正在嘗試在我的應用程序中實施Google+登錄,但它不起作用。嘗試使用Google+登錄時Google Play服務無法使用

每次我點擊登錄,onConnectionFailed會在我選擇帳戶後立即被調用。

有人能讓我知道有什麼問題嗎?

public class LoginActivity extends ActionBarActivity 
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener{ 

/* 
Variables 
*/ 

/* Request code used to invoke sign in user interactions. */ 
private static final int RC_SIGN_IN = 0; 

/* Client used to interact with Google APIs. */ 
private GoogleApiClient mGoogleApiClient; 

/* A flag indicating that a PendingIntent is in progress and prevents 
* us from starting further intents. 
*/ 
private boolean mIntentInProgress; 

/* 
* True if the sign-in button was clicked. When true, we know to resolve all 
* issues preventing sign-in without waiting. 
*/ 
private boolean mSignInClicked; 

/* 
Lifecycle 
*/ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_login); 

    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Plus.API, Plus.PlusOptions.builder().build()) 
      .addScope(Plus.SCOPE_PLUS_LOGIN) 
      .build(); 

    // Sign in button click listener 
    findViewById(R.id.googleSignInButton).setOnClickListener(this); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    mGoogleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

    if (mGoogleApiClient.isConnected()) { 
     mGoogleApiClient.disconnect(); 
    } 
} 

/* 
Callbacks 
*/ 

@Override 
public void onClick(View v) { 
    if (v.getId() == R.id.googleSignInButton && !mGoogleApiClient.isConnecting()) { 
     mSignInClicked = true; 
     mGoogleApiClient.connect(); 
    } 
} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    Log.i("TAG", "onConnectionFailed"); 
    if (!mIntentInProgress) { 
     if (mSignInClicked && connectionResult.hasResolution()) { 
      // The user has already clicked 'sign-in' so we attempt to resolve all 
      // errors until the user is signed in, or they cancel. 
      try { 
       connectionResult.startResolutionForResult(this, RC_SIGN_IN); 
       mIntentInProgress = true; 
      } catch (IntentSender.SendIntentException e) { 
       // The intent was canceled before it was sent. Return to the default 
       // state and attempt to connect to get an updated ConnectionResult. 
       mIntentInProgress = false; 
       mGoogleApiClient.connect(); 
      } 
     } 
    } 
} 

@Override 
public void onConnected(Bundle bundle) { 
    Log.i("TAG", "onConnected"); 

    mSignInClicked = false; 
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i("TAG", "onConnectionSuspended"); 
    mGoogleApiClient.connect(); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RC_SIGN_IN) { 
     if (resultCode != RESULT_OK) { 
      mSignInClicked = false; 
     } 

     mIntentInProgress = false; 

     if (!mGoogleApiClient.isConnected()) { 
      mGoogleApiClient.reconnect(); 
     } 
    } 
} 
} 

我甚至下載了官方的Google樣本登錄,我得到了同樣的錯誤。它只是不會登錄和連接。

我有良好的連接(甚至無線),我嘗試了在多個手機上。

+0

可能與您生成的密鑰存在一些問題。請嘗試生成新密鑰,然後重試。 –

+0

是的!我必須再次使用debug.keystore生成密鑰。我以前用我的官方密鑰倉庫完成了它。謝謝!隨意張貼一個答案,我可以接受它:) – Guy

+0

Stacktrace請。 :) – theapache64

回答

0

這可能是您使用的密鑰的問題。 轉到您的谷歌API的控制檯嘗試生成一個新的Cient ID與您的 SHA1從debug.keystore獲得,並嘗試再次登錄。我相信它會幫助解決您的問題。

相關問題