Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign In - ApiException 12501 when dismissing the sign in dialog

I have implemented the Google Sign In successfully, it works fine and I get no exceptions when I actually go through the whole sign in process.

But if I click on the Google sign in button and then hit the back button to dismiss the dialog, I somehow get the following error:

Caused by: com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 12501: at com.google.android.gms.tasks.zzu.getResult(Unknown Source:17)

which points me to this piece of code:

if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                val account = task.result
                firebaseAuthWithGoogle(account)
            } catch (e: ApiException) {
                Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
            }
        }

specifically on the val account = task.result line.

My question is, shouldn't it handle it by himself via the catch and print out the toast? Why is the whole app crashing instead?

Here is my whole SignInActivity:

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.BottomSheetDialog
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Toast
import com.dancam.subscriptions.ActiveSubscriptions.SubscriptionsMain
import com.dancam.subscriptions.R.id.google_signin_button
import com.dancam.subscriptions.R.id.signin_progressbar
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.SignInButton
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import kotlinx.android.synthetic.main.reset_password_dialog.view.*
import kotlinx.android.synthetic.main.sign_dialog.view.*
import kotlinx.android.synthetic.main.signin.*
import org.jetbrains.anko.defaultSharedPreferences
import org.jetbrains.anko.sdk25.coroutines.onClick

class SignInActivity: AppCompatActivity() {
    private var mAuth: FirebaseAuth? = null
    private var mGoogleSignInClient: GoogleSignInClient? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.signin)

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build()

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
        mAuth = FirebaseAuth.getInstance()

        google_signin_button.setSize(SignInButton.SIZE_WIDE)
        google_signin_button.setOnClickListener { signIn() }
    }

    override fun onStart() {
        super.onStart()
        val currentUser = mAuth?.currentUser

        if (currentUser!=null) loggedIn(currentUser)
    }

    private fun signIn() {
        val signInIntent = mGoogleSignInClient?.signInIntent
        startActivityForResult(signInIntent, RC_SIGN_IN)
        signin_progressbar.visibility = View.VISIBLE
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            try {
                val account = task.result
                firebaseAuthWithGoogle(account)
            } catch (e: ApiException) {
                Toast.makeText(this, "Google Sign In Failed", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
        val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
        mAuth?.signInWithCredential(credential)
                ?.addOnCompleteListener {
                    if (it.isSuccessful) {
                        signin_progressbar.visibility = View.GONE
                        val user = mAuth?.currentUser
                        loggedIn(user)
                    } else {
                        signin_progressbar.visibility = View.GONE
                        Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
                        loggedIn(null)
                    }
                }
    }

    fun onSignUpClicked(args: View) {
        val view = layoutInflater.inflate(R.layout.sign_dialog, null)
        val dialog = BottomSheetDialog(this)
        dialog.setContentView(view)
        dialog.show()

        view.register_button.setOnClickListener {
            val email = view.signup_email.text.toString()
            val password = view.signup_password.text.toString()
            val password2 = view.signup_password2.text.toString()

            if (password == password2) {
                mAuth?.createUserWithEmailAndPassword(email, password)
                        ?.addOnCompleteListener {
                            if (it.isSuccessful) {
                                //sign in success updateUI
                                val user = mAuth?.currentUser
                                loggedIn(user)
                            } else {
                                // signin failed
                                //TODO: translate
                                Toast.makeText(this, "Authentication Failed", Toast.LENGTH_SHORT).show()
                                loggedIn(null)
                            }
                        }
            } else {
                //TODO: translate
                Toast.makeText(this, "Passwords do not match", Toast.LENGTH_LONG).show()
            }
        }
    }

    fun onSignInClicked(args: View) {
        val view = layoutInflater.inflate(R.layout.sign_dialog, null)
        val dialog = BottomSheetDialog(this)

        //TODO: translate
        view.dialog_textview.text = "Sign In"
        view.signup_password2.visibility = View.GONE

        dialog.setContentView(view)
        dialog.show()

        //TODO: translate
        view.register_button.text = "Sign In"
        view.register_button.setOnClickListener {
            val email = view.signup_email.text.toString()
            val password = view.signup_password.text.toString()

            mAuth?.signInWithEmailAndPassword(email, password)
                    ?.addOnCompleteListener {
                        if (it.isSuccessful) {
                            val user = mAuth?.currentUser
                            loggedIn(user)
                        } else {
                            //TODO: translate
                            Toast.makeText(this, "Sign In Failed", Toast.LENGTH_LONG).show()
                            view.forgotten_password.visibility = View.VISIBLE

                            view.forgotten_password.onClick {
                                dialog.dismiss()
                                resetDialog()
                            }
                        }
                    }
        }
    }

    private fun resetDialog() {
        val resetView = layoutInflater.inflate(R.layout.reset_password_dialog, null)
        val resetDialog = BottomSheetDialog(this)
        resetDialog.setContentView(resetView)
        resetDialog.show()

        resetView.send_reset_email.setOnClickListener {
            resetView.send_reset_email.isClickable = false
            val email = resetView.reset_email.text.toString()
            mAuth?.sendPasswordResetEmail(email)
                    ?.addOnSuccessListener {
                        //TODO: translate
                        Toast.makeText(this, "Email Sent", Toast.LENGTH_SHORT).show()
                        resetDialog.dismiss()
                    }
        }
    }

    private fun loggedIn(user: FirebaseUser?) {
        startActivity(Intent(this, SubscriptionsMain::class.java))
        finish()
    }

    fun onSkipClicked(args:View) {
        loggedIn(null)
    }

    companion object {
        const val RC_SIGN_IN = 0000    }
}
like image 613
Daniele Avatar asked May 21 '18 11:05

Daniele


2 Answers

Can you try this and let me know what happens:

if (requestCode == RC_SIGN_IN) {
        val task = GoogleSignIn.getSignedInAccountFromIntent(data)
        try {
            val account = task.result(ApiException::class.java)
            firebaseAuthWithGoogle(account)
        } catch (e: ApiException) {
            Log.w("TAG", "signInResult:failed code=" + e.getStatusCode());
        }
    }

Please, tell me what you get when Logging this.

Also it would be great if you can also upload whole code you wrote in SignIn Activity and also the google-services.json file so that i can debug the issue.

EDIT Add this

if (resultCode == Activity.RESULT_OK)

after requestCode == RC_SIGN_IN

like image 174
Keshav Aggarwal Avatar answered Sep 22 '22 10:09

Keshav Aggarwal


Better check google sample and handle exception like this

private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);

            // Signed in successfully, show authenticated UI.
            updateUI(account);
        } catch (ApiException e) {
            // The ApiException status code indicates the detailed failure reason.
            // Please refer to the GoogleSignInStatusCodes class reference for more information.
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
            updateUI(null);
        }
    }

Such way you will not lose sign in status codes and can check for common status code.

like image 25
MainActivity Avatar answered Sep 21 '22 10:09

MainActivity