'Google Sign in on android either gives error code or activity doesn't return

I'm implementing Google Sign in on a flutter app. I'm using method channels and the official google implementation of OAuth sign in.

But the activity sometimes doesn't return onActivityResult, and when it does I get error code 10.

Here is the Google Login Class I made:

package com.example.app

import android.app.Activity
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 android.content.Intent
import android.util.Log
import android.widget.Toast
import com.google.android.gms.tasks.Task
import com.google.android.gms.common.api.ApiException


class GoogleLogin(val activity: Activity,val GOOGLE_LOGIN:Int) {
private val mGoogleSignInClient:GoogleSignInClient

val TAG="GoogleLogin"

init {
    Log.d(TAG, ": ")
    val gso: GoogleSignInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(activity.resources.getString(R.string.google_client_id))
        .requestId()
        .build()
    mGoogleSignInClient = GoogleSignIn.getClient(activity, gso)
}

fun signIn() {
    val signInIntent = mGoogleSignInClient.signInIntent
    activity.startActivityForResult(signInIntent, GOOGLE_LOGIN)
    Log.d(TAG, "signIn: ")
    Toast.makeText(activity,"signIn: ",Toast.LENGTH_SHORT).show()

}


private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>): String? {
    Log.d(TAG, "handleSignInResult")
    Toast.makeText(activity,"handleSignInResult",Toast.LENGTH_SHORT).show()
    var idToken:String?=null;
    try {
        val account = completedTask.getResult(ApiException::class.java)
        Toast.makeText(activity,"got activity result checking id token",Toast.LENGTH_SHORT).show()

        idToken=account.idToken
    } catch (e: ApiException) {
        Toast.makeText(activity,"signInResult:failed code=" + e.statusCode,Toast.LENGTH_SHORT).show()

        Log.e(TAG, "signInResult:failed code=" + e.statusCode)
    }

    return idToken;
}

fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) :String? {
    if (requestCode == GOOGLE_LOGIN) {
        val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(data)
        return handleSignInResult(task)
    }
    return null
}

}

Here is the code that calls it

if (call.method == "google_login") {
            val account=GoogleSignIn.getLastSignedInAccount(activity)
            if(account!=null) {
                if(account.idToken!=null){
                    result.success(account.idToken)
                    return@setMethodCallHandler
                }
                Toast.makeText(activity,"account already signed in but no token id",Toast.LENGTH_SHORT).show()

            }
            else{
                Toast.makeText(activity,"account doesn't exist",Toast.LENGTH_SHORT).show()
            }

            googleLogin = GoogleLogin(this, GOOGLE_LOGIN)
            googleLogin.signIn()

        }

Here is the onActivityResult:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    Log.d(TAG, "onActivityResult: $requestCode")
    Toast.makeText(activity,"onActivityResult: $requestCode",Toast.LENGTH_LONG).show()

    if(requestCode== GOOGLE_LOGIN){
           val result= googleLogin.onActivityResult(requestCode,resultCode,data)
            if(result==null){
                Toast.makeText(activity,"got activity result but id token is null  or there was an error",Toast.LENGTH_SHORT).show()
                result_flutter.error("","Failed to Sign In Google",null)
          }
          else{
                Toast.makeText(activity,"cool it all works",Toast.LENGTH_SHORT).show()

                result_flutter.success(result)
          }
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source