2

I have an app in which i have implemented google+ sign in. I have checked all the code and found after dubugging it is always throws an error in onConnectionFailed(ConnectionResult result) where result is shown as follow:

ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{425b8550: android.os.BinderProxy@423ec2e8}, message=null}

code:-

mGoogleApiClient = buildGoogleAPIClient();
gPlusLoginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            processGPlusSignIn();
        }
    });


 private void processGPlusSignIn() {
    if (!mGoogleApiClient.isConnecting()) {
        Log.e("", "GPLUS area 111");
        startExecutingGPlusLoginProcess();
        mSignInClicked = true;
    }

}
private void startExecutingGPlusLoginProcess() {
    if (mConnectionResult != null && mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            Log.i("Registration", "Starting...");
            mConnectionResult.startResolutionForResult(this, GPLUS_SIGN_IN_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
            Log.e("Registartion", "Exception***" + e);
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG,"onConnectionFailed called");

    if (!connectionResult.hasResolution()) {
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, ERROR_DIALOG_REQUEST_CODE).show();
        return;
    }
    if (!mIntentInProgress) {
        mConnectionResult = connectionResult;
        Log.e("Registration", "Result?***" + connectionResult);
        if (mSignInClicked) {
            startExecutingGPlusLoginProcess();
        }
    }
}
Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
Ragvender
  • 67
  • 2
  • 7

1 Answers1

0

You may try the below code : first you need to initalize GoogleApi like that:

private void googleInitialization() {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    mGoogleApiClient.connect();


}

After that you need to call this method like that:-

googleInitialization();
                    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
                    startActivityForResult(signInIntent, RC_SIGN_IN);

and thid handle in OnActivityResult there is a CallBackManager which is very useful to call the login button

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

        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }

Now, Get result in the below method:

 private void handleSignInResult(GoogleSignInResult result) {
        Log.d("handleSignInResult", "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            String personName = acct.getDisplayName();
            String email = acct.getEmail();
            if (personName.contains(" ")) {
              String  fname = personName.substring(0, personName.lastIndexOf(" ") + 1);
             String     lname = personName.substring(personName.lastIndexOf(" ") + 1);
                Log.e("First name", fname);
                Log.e("Last name", lname);
            } else
              String    fname = personName;

            Log.e("Profile Info", "Name: " + personName + ", email: " + email);
        }
    }
PRIYA PARASHAR
  • 777
  • 4
  • 15