Can't use FirebaseUI with fragments

2019-08-20 03:12发布

问题:

I'm trying to implement FirebaseAuth UI to my app, but I've ran into some issues.

I'm trying to create the FirebaseUIActivity.kt as a fragment, but when I call the support fragment manager it crashes.

  supportFragmentManager.beginTransaction().replace(R.id.fragment_container, FirebaseUIActivity.newInstance()).commit()

Here is some code:

MainActivity.kt

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

    // Support toolbar
    setSupportActionBar(binding.toolbar)

    supportFragmentManager.beginTransaction().replace(R.id.fragment_container, FirebaseUIActivity.newInstance()).commit()
    }

}

FirebaseUiActivity.kt

class FirebaseUIActivity : Fragment() {
// Choose authentication providers

companion object {
    fun newInstance() = FirebaseUIActivity()
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val binding: FirebaseUiFragmentBinding = DataBindingUtil.inflate(inflater, R.layout.firebase_ui_fragment, container, false)

    return binding.root

}

override fun onResume() {
    super.onResume()
}
private val providers = arrayListOf(
    AuthUI.IdpConfig.EmailBuilder().build(),
    AuthUI.IdpConfig.GoogleBuilder().build()
)

override fun startActivityForResult(intent: Intent?, requestCode: Int) {
    super.startActivityForResult(intent, requestCode)

    AuthUI.getInstance().createSignInIntentBuilder()
        .setAvailableProviders(providers)
        .build()

    }
}

Error:

     Caused by: java.lang.IllegalStateException: Check your google-services plugin configuration, the default_web_client_id string wasn't populated.
    at com.firebase.ui.auth.util.Preconditions.checkConfigured(Preconditions.java:90)
    at com.firebase.ui.auth.AuthUI$IdpConfig$GoogleBuilder.<init>(AuthUI.java:957)
    at com.example.katzinbetihot.Firebase.FirebaseUIActivity.<init>(FirebaseUIActivity.kt:40)
    at com.example.katzinbetihot.Firebase.FirebaseUIActivity$Companion.newInstance(FirebaseUIActivity.kt:21)
    at com.example.katzinbetihot.main.MainActivity.onCreate(MainActivity.kt:24)
    at android.app.Activity.performCreate(Activity.java:7149)
    at android.app.Activity.performCreate(Activity.java:7140)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1288)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3031)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3191) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1920) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6912) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Thanks for your help in advance, Emek!

回答1:

Try adding this code line to your strings.xml file

<string name="default_web_client_id" translatable="false">webClientId.apps.googleusercontent.com</string>

You can get your WEB CLIENT ID from Google Console. Select your project, then go to API & Services > Credentials and under OAuth 2.0 client IDs you will find a ID with the name Web Client. Copy and replace the above code with it.

If a Web Client ID does not exist, you can click on Create Credentials > OAuth client ID then select Web Application as the application type on the next page and click create.



回答2:

This is to help those who have a similar situation as I did (changing google-services.json and this error started)

TL;DR: compare your old version to the new version and add the web client id part (can be found in GCP console) into the oauth-client section of the new file.

I got the same problem, but it was working fine for a long time until recently when I updated my code with a new google-services.json. I reverted to the older version of the google-services.json file and it worked.

So I checked into the difference between the two versions, they were only slightly different. Following @Totoo's answer, I went to my GCP console and got my Web client ID (called "Web client (auto created by Google Service)"), I searched the ID in both versions of the google-services.json file, and it exists in both files under the services/appinvite_service/other_platform_oauth_client section, but only also exists under the oauth_client section in the older (working) version of the file. That section looks like this:

"oauth_client": [
    ...,
    {
         "client_id": "CLIENT_ID (ends with apps.googleusercontent.com)",
         "client_type": 3
    }
]

So, I added this part into the new version of the google-services.json file and it worked.

I have only changed the signing SHA signatures in Firebase console (which is the reason I went and downloaded a new version of the file at the first place), so I suspect this is some sort of bug on Firebase's end with them generating the google-services.json file, of course, I could be wrong, so feel free correct me.