Hi i am having a problem with my the google connexion. Every time that the code is executed it goes in to the catch. I dont know why. I followed the steps from the Google tutorial and saw manyq questions related to that on stack but still not help me to solve this issue. Whe the code is runned it goes in to the catch and it shows the toast that i made. " error while.. ".
Here is what it looks like. I am posting the whole code for the authentification so everyone can analyze it and give me a solution if possible.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient= GoogleSignIn.getClient(this,gso);
signInButton=(SignInButton)findViewById(R.id.googleButton);
signInButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
//pour google
mAuthStateListener=new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser mFireBaseUser=mFirebaseAuth.getCurrentUser();
if(mFireBaseUser != null){
Toast.makeText(LoginActivity.this,"you are logged in",Toast.LENGTH_SHORT).show();
Intent i= new Intent(LoginActivity.this, PrayersEvents.class);
startActivity(i);
}
else{
Toast.makeText(LoginActivity.this,"please login",Toast.LENGTH_SHORT).show();
}
}
};
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email=emailId.getText().toString();
String pwd=password.getText().toString();
if (email.isEmpty()) {
emailId.setError("Please enter email");
emailId.requestFocus();
}
else if(pwd.isEmpty()){
password.setError("Please enter your password");
password.requestFocus();
}
else if(email.isEmpty() || pwd.isEmpty()){
Toast.makeText(LoginActivity.this,"Fields are empty!",Toast.LENGTH_SHORT).show();
}
else if(!(email.isEmpty() && pwd.isEmpty())){
mFirebaseAuth.signInWithEmailAndPassword(email,pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"Login error!",Toast.LENGTH_SHORT).show();
}
else{
Intent intoHome=new Intent(LoginActivity.this, PrayersEvents.class);
startActivity(intoHome);
}
}
});
}
else{
Toast.makeText(LoginActivity.this,"Error ocurred!",Toast.LENGTH_SHORT).show();
}
}
});
tvSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intoSignUp=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intoSignUp);
}
});
}
@Override
protected void onStart() {
super.onStart();
GoogleSignInAccount account=GoogleSignIn.getLastSignedInAccount(this);
if(account !=null){
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d("MON_TAG", account.toString());
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Toast.makeText(getApplicationContext(),"error while logging in",Toast.LENGTH_SHORT).show();
Log.w("errorMsg","Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {//acct means account
Log.d("MON_TAG", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("MON_TAG", "signInWithCredential:success");
FirebaseUser user = mFirebaseAuth.getCurrentUser();
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
Toast.makeText(getApplicationContext(),"You are now connected",Toast.LENGTH_SHORT).show();
} else {
GoogleSignInAccount account=GoogleSignIn.getLastSignedInAccount(getApplicationContext());
if(account !=null){
startActivity(new Intent(LoginActivity.this, CreateEvent.class));
}
// If sign in fails, display a message to the user.
Toast.makeText(getApplicationContext(),"Could not log in user",Toast.LENGTH_SHORT).show();
}
}
});
}
I think you didn't instanciate the GoogleApiClient object..
try this code.. it worked for me..
//a constant for detecting the login intent result
private static final int RC_SIGN_IN = 234;
//Tag for the logs optional
private static final String TAG = "YoussefIslem";
//creating a GoogleSignInClient object
GoogleSignInClient mGoogleSignInClient;
GoogleApiClient googleApiClient;
CallbackManager mCallbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.web_client_id))
.requestEmail()
.build();
googleApiClient =new GoogleApiClient.Builder(this)
.enableAutoManage(this,this)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
//Then we will get the GoogleSignInClient object from GoogleSignIn class
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
findViewById(R.id.googleContinue).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
//getting the auth credential
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
//Now using firebase we are signing in the user here
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(MainActivity.this, "User Signed In", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
finish();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(MainActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// ...
}
});
}
//this method is called on click
private void signIn() {
//getting the google signin intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
googleApiClient.clearDefaultAccountAndReconnect();
//starting the activity for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//if the requestCode is the Google Sign In code that we defined at starting
if (requestCode == RC_SIGN_IN) {
//Getting the GoogleSignIn Task
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
//Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
//authenticating with firebase
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(MainActivity.this,"I'm here"+ e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.