google sign in Intent doesn't return RESULT_OK in start activity for result

Viewed 1513

First I create a callback using New Api to get Activity for result

private val getGoogleDataBack=registerForActivityResult(ActivityResultContracts.StartActivityForResult(),
  ActivityResultCallback {

      if (it.resultCode == RESULT_OK) {
          Log.d(TAG, "------------------ACTIVITY START CALLBACK--------------------: ")
          val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
          var userToken: String? = null

          try {
              var account = task.getResult(ApiException::class.java)
              if (account != null) {
                  userToken = account.idToken
                  Log.d(TAG, "----------------------------$userToken: ")
              } else Log.d(TAG, "ACCOUNT IS NULL: ")
          } catch (e: Exception) {
              Log.d(TAG, ":FAILED TO GET USER ${e.message}")
          }

          viewModel.handleEvent(LoginEvent.OnGoogleSignInResult(LoginResult(123, userToken!!)))
      }
      else{
          Log.d(TAG, "------------------RESULT ISN'T OKAY ${it.resultCode}  ----------------: ")
      }
  })

and here's the google sign in code

private fun startSignInFlow() {
    Log.d(TAG, "startSignInFlow: ---------------START SIGN IN FLOW")
    val gso=GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.clientId))
        .build()
     val gSignInClient=GoogleSignIn.getClient(requireActivity(),gso)
      val signInIntent= gSignInClient.signInIntent
    getGoogleDataBack.launch(signInIntent)

}

Manifest

  <activity android:name=".ui.MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

It opens the google activity then I choose an Email but when activty for result callback get called it always enter the else where resultCode ISN'T OKAY can somebody help me?
logcat

 AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.noteapp, PID: 13979
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=860860250, result=0, data=Intent { (has extras) }} to activity {com.example.noteapp/com.example.noteapp.ui.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4268)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312)
    at android.app.ActivityThread.-wrap19(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NullPointerException
    at com.example.noteapp.ui.AuthenticationFragment$getGoogleDataBack$1.onActivityResult(AuthenticationFragment.kt:49)
    at com.example.noteapp.ui.AuthenticationFragment$getGoogleDataBack$1.onActivityResult(AuthenticationFragment.kt:26)
    at androidx.activity.result.ActivityResultRegistry.doDispatch(ActivityResultRegistry.java:344)
    at androidx.activity.result.ActivityResultRegistry.dispatchResult(ActivityResultRegistry.java:308)
    at androidx.activity.ComponentActivity.onActivityResult(ComponentActivity.java:618)
    at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)
    at android.app.Activity.dispatchActivityResult(Activity.java:7276)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4264)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4312) 
    at android.app.ActivityThread.-wrap19(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1644) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
1 Answers

You will have a couple of OAuth_2.0 ids, the one which should be used is the Web Client auth Id. Secondly check if your app's SHA1/SHA256 is in firebase app fingerprints.

IDs

Related