0

I'm trying to implement Google+ login in my application but it won't work.

Everytime I click log in, the onConnectionFailed gets called as soon as I choose the account.

Could someone please let me know what's wrong?

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();
        }
    }
}
}

I even downloaded official Google sample for login and I get the same error. It just won't log in and connect.

I have good connection (even wifi) and I tried it on multiple phones.

Guy
  • 6,414
  • 19
  • 66
  • 136

1 Answers1

0

It just might be an issue with the key you are using. Go to your google api's console Try Generating a new Cient id with your SHA1 obtained from debug.keystore and try Login again.I'm sure it'll help solve your issue.

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42