GoogleSignInResult isSuccess failed

前端 未结 6 2007
栀梦
栀梦 2020-12-10 17:09

I want to use the Google Login for my App, but I always get the message Status{statusCode=unknown status code: 12500, resolution=null and the login failed. Has

6条回答
  •  臣服心动
    2020-12-10 17:35

    [https://developers.google.com/identity/sign-in/android/sign-in ] follow this api documentation but keep in mind that inside WEB_CLIENT_ID use the value of client id which is generated inside google-services.json file.

    class MainActivity : AppCompatActivity(), GoogleApiClient.OnConnectionFailedListener {
    
    private val TAG = "JSAGoogleSignIn"
    private val REQUEST_CODE_SIGN_IN = 1234
    //  private val WEB_CLIENT_ID = "969826080188-rs95o2iilt1l9u6g1su0hf271ml4d5bj.apps.googleusercontent.com"
    private val WEB_CLIENT_ID = "354298333018-hh9bnkfuf0meimf2n2u0pio7dh8mg273.apps.googleusercontent.com"
    private var mAuth: FirebaseAuth? = null
    
    private var mGoogleApiClient: GoogleApiClient? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var txt_register = findViewById(R.id.txt_register)
        txt_register.setOnClickListener {
            var intent = Intent(this@MainActivity, RegisterActivity::class.java)
            finish()
            startActivity(intent)
        }
        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(WEB_CLIENT_ID)
                .requestEmail()
                .build()
        mGoogleApiClient = GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build()
    
        mAuth = FirebaseAuth.getInstance()
        sign_in_button.setOnClickListener {
            val intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient)
            startActivityForResult(intent, REQUEST_CODE_SIGN_IN)
        }
    
    }
    
    
    override fun onConnectionFailed(p0: ConnectionResult) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }
    
    
    private fun updateUI(user: FirebaseUser?) {
        if (user != null) {
            Log.e("Email", "Value" + user.email)
        }
    
    }
    
    fun signIn() {
    
    }
    
    override fun onStart() {
        super.onStart()
        val currentUser = mAuth!!.currentUser
        updateUI(currentUser)
    }
    
    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    
        // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
        if (requestCode == REQUEST_CODE_SIGN_IN) {
            val result = Auth.GoogleSignInApi.getSignInResultFromIntent(data)
            if (result.isSuccess) {
                // successful -> authenticate with Firebase
                val account = result.signInAccount
                firebaseAuthWithGoogle(account!!)
            } else {
                // failed -> update UI
                updateUI(null)
                Toast.makeText(applicationContext, "SignIn: failed!" + result.status,
                        Toast.LENGTH_SHORT).show()
            }
        }
    }
    
    private fun firebaseAuthWithGoogle(acct: GoogleSignInAccount) {
        Log.e(TAG, "firebaseAuthWithGoogle():" + acct.id!!)
    
        val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
        mAuth!!.signInWithCredential(credential)
                .addOnCompleteListener(this) { task ->
                    if (task.isSuccessful) {
                        // Sign in success
                        Log.e(TAG, "signInWithCredential: Success!")
                        val user = mAuth!!.currentUser
                        updateUI(user)
                    } else {
                        // Sign in fails
                        Log.w(TAG, "signInWithCredential: Failed!", task.exception)
                        Toast.makeText(applicationContext, "Authentication failed!",
                                Toast.LENGTH_SHORT).show()
                        updateUI(null)
                    }
                }
    }
    

提交回复
热议问题