How to sign in user and retrieve token with Google Api Client

219 views Asked by At

I am trying to let the user login in my app with his google account.

I setup the client id in the google developer console, with the package name and the sha1 of my debug keystore, enabled the google plus api and filled in the consent screen.

Here is the code of the activity which perform the login with googleApiClient:

public class IntroActivity extends BaseActivity implements
    LoginFragment.FragmentListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final int RC_GOOGLE_SIGN_IN = 0;

private GoogleApiClient googleApiClient;
private boolean intentInProgress;

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

    if (savedInstanceState == null) {
        LoginFragment loginFragment = new LoginFragment();
        replaceFragment(loginFragment, false);
    }

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

@Override
protected void onStop() {
    super.onStop();
    if (googleApiClient.isConnected()) {
        googleApiClient.disconnect();
    }
}

@Override
protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
    switch (requestCode) {
        case RC_GOOGLE_SIGN_IN:
            intentInProgress = false;
            if (!googleApiClient.isConnecting()) {
                googleApiClient.connect();
            }
            break;
    }
}

@Override
public void onGoogleSignInClick() {
    googleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    if (Plus.PeopleApi.getCurrentPerson(googleApiClient) != null) {
        String email = Plus.AccountApi.getAccountName(googleApiClient);
        String token = null;
        try {
            token = GoogleAuthUtil.getToken(this, email, Plus.SCOPE_PLUS_LOGIN.toString());
        } catch (IOException | GoogleAuthException e) {
            e.printStackTrace();
        }

        showShortToast(token);
    }
}

@Override
public void onConnectionSuspended(int i) {
    googleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if (!intentInProgress && connectionResult.hasResolution()) {
        try {
            intentInProgress = true;
            startIntentSenderForResult(connectionResult.getResolution().getIntentSender(), RC_GOOGLE_SIGN_IN, null, 0, 0, 0);
        } catch (IntentSender.SendIntentException e) {
            intentInProgress = false;
            googleApiClient.connect();
        }
    } else {
        showShortToast("onConnectionFailed");
    }
}
}

As soon as the googleApiClient starts connecting, it fails to connect and it displays a popup in order to choose the account. Once selected, it still goes in onConnectionFailed method, but this time seems there is no resolution for the problem.

The ConnectionResult get {statusCode=SIGN_IN_FAILED, resolution=null}

Why I get sign in failed? Is it possible that the app is not linked with the project on the dev console somehow?

0

There are 0 answers