0

Tried implementing Google sign up for android app, the code is as below:

public class MainActivity extends AppCompatActivity implements View.OnClickListener, GoogleApiClient.OnConnectionFailedListener {

    //Signin button
    private SignInButton signInButton;

    //Signing Options
    private GoogleSignInOptions gso;

    //google api client
    private GoogleApiClient mGoogleApiClient;

    //Signin constant to check the activity result
    private int RC_SIGN_IN = 100;

    //TextViews
    private TextView textViewName;
    private TextView textViewEmail;
    private NetworkImageView profilePhoto;

    //Image Loader
    private ImageLoader imageLoader;

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

        //Initializing Views
        textViewName = (TextView) findViewById(R.id.textViewName);
        textViewEmail = (TextView) findViewById(R.id.textViewEmail);
        profilePhoto = (NetworkImageView) findViewById(R.id.profileImage);

        //Initializing google signin option
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        //Initializing signinbutton
        signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_WIDE);
        signInButton.setScopes(gso.getScopeArray());

        //Initializing google api client
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();


        //Setting onclick listener to signing button
        signInButton.setOnClickListener(this);
    }


    //This function will option signing intent
    private void signIn() {
        //Creating an intent
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);

        //Starting intent for result
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //If signin
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            //Calling a new function to handle signin
            handleSignInResult(result);
        }
    }


    //After the signing we are calling this function
    private void handleSignInResult(GoogleSignInResult result) {
        //If the login succeed
        if (result.isSuccess()) {
            //Getting google account
            GoogleSignInAccount acct = result.getSignInAccount();

            //Displaying name and email
            textViewName.setText(acct.getDisplayName());
            textViewEmail.setText(acct.getEmail());


        } else {
            //If login fails
            Toast.makeText(this, "Login Failed", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onClick(View v) {
        if (v == signInButton) {
            //Calling signin
            signIn();
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}

I have rechecked all the prerequisites as per the google directives but I am getting a login failed toast as the result.isSuccess() is false again and again. I have seen many people wondering with this error but no clear solution anywhere. Is there something obvious I am missing?

mastov
  • 2,942
  • 1
  • 16
  • 33
gunner
  • 3
  • 2

2 Answers2

1

You're not connecting the GoogleApiClient. Acc to the docs here:

You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.

So, you need to connect() and subsequently disconnect() the GoogleApiClient.

gsb
  • 5,520
  • 8
  • 49
  • 76
  • Tried implementing that still getting the same result. – gunner May 06 '16 at 17:38
  • Have you tried looking through the logs to see why is it failing? Usually, there are logs that are printed when there is an error. @gunner – gsb May 06 '16 at 18:00
  • there are no errors being shown in the log 05-06 23:31:38.090 29036-29066/app.num.googleintegration I/GMPM﹕ Tag Manager is not found and thus will not be used 05-06 23:31:38.232 29036-29036/app.num.googleintegration D/SignInActivity﹕ handleSignInResult:false 05-06 23:31:38.232 29036-29036/app.num.googleintegration D/AppTracker﹕ App Event: start 05-06 23:31:38.279 29036-29036/app.num.googleintegration D/AbstractTracker﹕ Event success 05-06 23:31:38.279 29036-29036/app.num.googleintegration I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@1380514d time:29261787 – gunner May 06 '16 at 18:02
  • it seems that there is no problem with the code because I have directly taken the google code as well as many codes through various tutorials, I am not getting any error but the code is not getting a response from the server. It even asks permission pop up but then handleSignInResult:false. Is there something missing in the google code as I have seen this issue on many forums but nowhere it is solved – gunner May 06 '16 at 18:10
0

You will have to create the signed APK for this. Make sure that you use a different SHA key for release. Then generate the APK and directly install it on your phone. It should work that way.

  • yeah Thanks or your answer. Something similar has been suggested in the link posted by nerd in the comments. However that link does not say about a different SHA.. let me look into this. – gunner May 06 '16 at 20:42