Firebase身份验证-初始化多个项目(FirebaseUiException:代码10) [英] Firebase Authentication - Initializing Multiple Projects (FirebaseUiException: Code 10)

查看:136
本文介绍了Firebase身份验证-初始化多个项目(FirebaseUiException:代码10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • Coinverse Android应用初始化多个Firebase项目允许用户下载此 GitHub 项目/stackoverflow.com/questions/57236063/open-sourcing-android-app-firebase-authentication/57237312#57237312">StackOverflow答案.
  • 允许用户设置自己的Firebase项目进行身份验证
  • 运行该应用程序的open构建版本,并使用Firebase AuthUI
  • 登录
  • 为共享的Firestore和Cloud功能启用第二个预设的Firebase项目
  • Initialize multiple Firebase projects for the Coinverse Android app to allow users to download the open sourced GitHub project as outlined in this StackOverflow answer.
  • Allow users to setup their own Firebase project for authentication
  • Run the open build variant of the app and login with Firebase AuthUI
  • Enable a second pre-set Firebase project for shared Firestore and Cloud Functions

对一个Firebase项目的身份验证有效,初始化第二个项目时会在下面抛出错误

 fun initialize(context: Context) {
    if (BuildConfig.BUILD_TYPE == open.name)
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN)
                        .setApiKey(APP_API_KEY_OPEN)
                        .setDatabaseUrl(DATABASE_URL_OPEN)
                        .setProjectId(PROJECT_ID_OPEN)
                        .setStorageBucket(STORAGE_BUCKET_OPEN)
                        .build(),
                open.name)
    Firebase.setAndroidContext(context)
    initializeRemoteConfig()
}

初始化多个Firebase项目时,可以对匿名用户进行身份验证

初始化两个Firebase项目并尝试Google登录时出错.

登录失败com.firebase.ui.auth.FirebaseUiException:代码:10,消息:10:

Sign in fail com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10:

完整日志

2019-07-29 16:34:08.130 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    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:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.134 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    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:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.211 12406-12406/app.coinverse.open E/AuthUI: A sign-in error occurred.
com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 
    at com.firebase.ui.auth.data.remote.GoogleSignInHandler.onActivityResult(GoogleSignInHandler.java:109)
    at com.firebase.ui.auth.ui.idp.SingleSignInActivity.onActivityResult(SingleSignInActivity.java:128)
    at android.app.Activity.dispatchActivityResult(Activity.java:7462)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4391)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4440)
    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
    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:1816)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 16:34:08.261 12406-12406/app.coinverse.open E/SignInDialogFragment: Sign in fail com.firebase.ui.auth.FirebaseUiException: Code: 10, message: 10: 

实施

MainActivity

if (BuildConfig.BUILD_TYPE == open.name) {
        //Configures user's Firebase project.
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN_PRIVATE)
                        .setApiKey(APP_API_KEY_OPEN_PRIVATE)
                        .setDatabaseUrl(DATABASE_URL_OPEN_PRIVATE)
                        .setProjectId(PROJECT_ID_OPEN_PRIVATE)
                        .build(),
                OPEN_PRIVATE)
        //Configures shared pre-set Firebase project.
        FirebaseApp.initializeApp(
                context,
                FirebaseOptions.Builder()
                        .setApplicationId(APP_ID_OPEN_SHARED)
                        .setApiKey(APP_API_KEY_OPEN_SHARED)
                        .setDatabaseUrl(DATABASE_URL_OPEN_SHARED)
                        .setProjectId(PROJECT_ID_OPEN)
                        .setStorageBucket(STORAGE_BUCKET_OPEN_SHARED)
                        .build(),
                open.name)
    }
    Firebase.setAndroidContext(context)
    initializeRemoteConfig()
}
// If build type is not 'open' configures Firebase Project from google-services.json in 'debug' src set.

SignInFragment

//Returns the relevant Firebase project.
fun firebaseApp(isOpenPrivateUser: Boolean)=
    if (BuildConfig.BUILD_TYPE != open.name) FirebaseApp.getInstance()
    else if (BuildConfig.BUILD_TYPE == open.name && isOpenPrivateUser)
            FirebaseApp.getInstance(OPEN_PRIVATE)
    else FirebaseApp.getInstance(open.name)

confirm.setOnClickListener {
    startActivityForResult(
        AuthUI.getInstance(firebaseApp(true)).createSignInIntentBuilder()
                .setAvailableProviders(listOf(AuthUI.IdpConfig.GoogleBuilder().build()))
                .build(),
            RC_SIGN_IN)
    }

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == RC_SIGN_IN)
        if (resultCode == Activity.RESULT_OK) {
            homeViewModel.setUser(getInstance().currentUser)
            dismiss()
        } else {
            Log.e(LOG_TAG, "Sign in fail ${IdpResponse.fromResultIntent(data)?.error}")
        }
}

推荐答案

解决方案

问题1

每个程序包只能与一个SHA-1键关联. 当删除其他Firebase项目并使用SHA-1键将 shared Firebase项目恢复为原始状态时,控制台会显示以下错误,表明存在问题.

Solutions

Issue 1

Each package may only be associated with one SHA-1 key. When removing the additional Firebase project and reverting the shared Firebase project to it's original state with the SHA-1 key the console provided the following error which revealed an issue.

此软件包名称和SHA-1的OAuth2客户端已存在项目

此错误消息最初未显示,对于第一次在具有相同Android包名称的Firebase项目中检测到相同的SHA-1键很有用.

This error message did not show originally, and would be useful to see the first time the same SHA-1 key is detected in a Firebase project with the same Android package name.

从其他具有相同程序包名称的Firebase项目中删除SHA-1键.在Android应用程序中使用两个Firebase项目时,它们不能使用相同的SHA-1键.

Remove the SHA-1 key from any other Firebase projects with the same package name. When using two Firebase projects in an Android application, they cannot use the same SHA-1 key.

不是从第二个新创建的项目中的 google-services.json 生成默认的Firebase项目.

The default Firebase project was not being generated from the google-services.json in the second newly created project.

在Android Studio中的 Build > Rebuild Project

Rebuild the project in Android Studio under Build > Rebuild Project

这篇关于Firebase身份验证-初始化多个项目(FirebaseUiException:代码10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆