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?