Google Plus Connection always returns SIGN_IN_FAILED

| | August 4, 2015

I’m trying to carry out the Google Plus login within my app but no matter what I do, if the Scope is set to Plus.SCOPE_PLUS_LOGIN, I always get a ConnectionResult whose ErrorCode is 17 (SIGN_IN_FAILED). On the other hand, if I pick Plus.SCOPE_PLUS_PROFILE the app gets connected but when I retrieve the Person object, this one is always null.

I have created a new project in the developer console, enable the Google+ API and created a Cliend ID that includes the proper SHA1 fingerprint for the debug version of the app and its package. I have also picked an email and product name.

enter image description here

I have also tried to rename the package of the app and update the project in the developer console, but I still get the same error.

The thing is that I have downloaded the Google example, created a project in my developer console for it and it works… but when I try to use the same class that does the job in my app, it always fails… really weird. I have also checked the Google + API usage section and it never handles any request sent by my app… whereas it does it for the Google example…

This is the code of the class that carries out the whole process.. as you can see it looks practically the same as the one in the Google example…

public class MainActivity extends FragmentActivity implements
    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<People.LoadPeopleResult>, View.OnClickListener {

private static final int STATE_DEFAULT = 0;
private static final int STATE_SIGN_IN = 1;
private static final int STATE_IN_PROGRESS = 2;

private static final int RC_SIGN_IN = 0;

private static final int DIALOG_PLAY_SERVICES_ERROR = 0;

private static final String SAVED_PROGRESS = "sign_in_progress";

private int mSignInError;

private SignInButton mSignInButton;
private Button mSignOutButton;
private Button mRevokeButton;
private TextView mStatus;
private ListView mCirclesListView;
private ArrayAdapter<String> mCirclesAdapter;
private ArrayList<String> mCirclesList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
    mSignOutButton = (Button) findViewById(R.id.sign_out_button);
    mRevokeButton = (Button) findViewById(R.id.revoke_access_button);
    mStatus = (TextView) findViewById(R.id.sign_in_status);
    mCirclesListView = (ListView) findViewById(R.id.circles_list);

    mSignInButton.setOnClickListener(this);
    mSignOutButton.setOnClickListener(this);
    mRevokeButton.setOnClickListener(this);

    mCirclesList = new ArrayList<String>();
    mCirclesAdapter = new ArrayAdapter<String>(
            this, R.layout.circle_member, mCirclesList);
    mCirclesListView.setAdapter(mCirclesAdapter);

    if (savedInstanceState != null) {
        mSignInProgress = savedInstanceState
                .getInt(SAVED_PROGRESS, STATE_DEFAULT);
    }

    mGoogleApiClient = buildGoogleApiClient();
}

private GoogleApiClient buildGoogleApiClient() {
    return new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, Plus.PlusOptions.builder().build())
                    //.addScope(Plus.SCOPE_PLUS_PROFILE)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

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

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

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt(SAVED_PROGRESS, mSignInProgress);
}

@Override
public void onClick(View v) {
    if (!mGoogleApiClient.isConnecting()) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                mStatus.setText("Sign in");
                resolveSignInError();
                break;
            case R.id.sign_out_button:
                Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
                mGoogleApiClient.disconnect();
                mGoogleApiClient.connect();
                break;
            case R.id.revoke_access_button:
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient);
                mGoogleApiClient = buildGoogleApiClient();
                mGoogleApiClient.connect();
                break;
        }
    }
}

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

    mSignInButton.setEnabled(false);
    mSignOutButton.setEnabled(true);
    mRevokeButton.setEnabled(true);

    Person currentUser = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

    Log.d("profile", "profile, name: " + currentUser.getName() + ", id: " + currentUser.getId());

    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

    mStatus.setText(String.format("Sign in",
            email));

    Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
            .setResultCallback(this);

    mSignInProgress = STATE_DEFAULT;
}

@Override
public void onConnectionFailed(ConnectionResult result) {

    Log.i(TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = "
            + result.getErrorCode());

    if (result.getErrorCode() == ConnectionResult.API_UNAVAILABLE) {
  } else if (mSignInProgress != STATE_IN_PROGRESS) {
        mSignInIntent = result.getResolution();
        mSignInError = result.getErrorCode();

        if (mSignInProgress == STATE_SIGN_IN) {
            resolveSignInError();
        }
    }

    onSignedOut();
}

private void resolveSignInError() {
    if (mSignInIntent != null) {
        try {
            mSignInProgress = STATE_IN_PROGRESS;
            startIntentSenderForResult(mSignInIntent.getIntentSender(),
                    RC_SIGN_IN, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            Log.i(TAG, "Sign in intent could not be sent: "
                    + e.getLocalizedMessage());
            mSignInProgress = STATE_SIGN_IN;
            mGoogleApiClient.connect();
        }
    } else {
        showDialog(DIALOG_PLAY_SERVICES_ERROR);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    switch (requestCode) {
        case RC_SIGN_IN:
            if (resultCode == RESULT_OK) {
                mSignInProgress = STATE_SIGN_IN;
            } else {
                mSignInProgress = STATE_DEFAULT;
            }

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
            break;
    }
}

@Override
public void onResult(People.LoadPeopleResult peopleData) {
    if (peopleData.getStatus().getStatusCode() == CommonStatusCodes.SUCCESS) {
        mCirclesList.clear();
        PersonBuffer personBuffer = peopleData.getPersonBuffer();
        try {
            int count = personBuffer.getCount();
            for (int i = 0; i < count; i++) {
                mCirclesList.add(personBuffer.get(i).getDisplayName());
            }
        } finally {
            personBuffer.close();
        }

        mCirclesAdapter.notifyDataSetChanged();
    } else {
        Log.e(TAG, "Error requesting visible circles: " + peopleData.getStatus());
    }
}

private void onSignedOut() {
    mSignInButton.setEnabled(true);
    mSignOutButton.setEnabled(false);
    mRevokeButton.setEnabled(false);

    mStatus.setText("Sign out");

    mCirclesList.clear();
    mCirclesAdapter.notifyDataSetChanged();
}

@Override
public void onConnectionSuspended(int cause) {
    ConnectionResult that we can attempt to resolve.
    mGoogleApiClient.connect();
}

Leave a Reply