【问题标题】:How to connect different firebase databases to release/beta/alpha versions?如何将不同的 firebase 数据库连接到 release/beta/alpha 版本?
【发布时间】:2018-07-09 12:45:22
【问题描述】:

我有一个应用程序应该有不同的 Firebase 数据库、用于发布和测试版的 Firebase 存储,但我无法将 Firebase 连接到多个项目,因为 SHA 证书指纹应该不同。

这是来自 Firebase 的错误:

同时,我无法使用不同的签名证书在 Google Play 应用上上传。

以下是来自 Google Play 控制台的错误:

是否有可能以某种方式为一个项目使用不同的 Firebase 数据库?两个版本都将进入 Google Play,都应该被签名,因此调试版本在当前情况下将无法工作。

【问题讨论】:

  • 在屏幕截图上,SHA1 密钥不是真实的

标签: android firebase google-play sha1 sha


【解决方案1】:

是的,一个项目可以有不同的 Firebase 数据库。 不要添加 SHA1 密钥。正如错误中提到的“您可以省略 SHA1”

在任何一个firebase项目中,进入 authentication -> SIGN-IN METHOD-> GOOGLE ,点击编辑选项,它将打开一个视图,如下图所示

Expand 'Whitelist client ids *' 点击添加,添加其他firebase项目的client Id,可以在google-services.json中找到。

"oauth_client": [
        {
          "client_id": "583957834728-jprivhot8johm3himgkmhqnnlmh1nldj.apps.googleusercontent.com",
          "client_type": 3
        }
      ],

首先为该其他 Firebase 项目初始化一个 FirebaseApp 实例

    FirebaseOptions options = new FirebaseOptions.Builder()
       .setApplicationId("1:530266078999:android:481c4ecf3253701e") // Required for Analytics.
       .setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Required for Auth.
       .setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB.
       .build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

applicationid、Apikey 和 URL 应该是二级数据库。这些可以在这里找到设置->项目设置->常规选项卡

验证辅助数据库。

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
            FirebaseApp app = FirebaseApp.getInstance("secondary");
            FirebaseAuth.getInstance(app).signInWithCredential(credential)
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {

                            // If sign in fails, display a message to the user. If sign in succeeds
                            // the auth state listener will be notified and logic to handle the
                            // signed in user can be handled in the listener.
                            if (!task.isSuccessful()) {
                                Log.w(TAG, getString(R.string.signin_authentication_failed_msg), task.getException());
                                Toast.makeText(SignInActivity.this,
                                        getString(R.string.signin_authentication_failed),
                                        Toast.LENGTH_SHORT).show();


                            }
                      else {
                               //do something - login sucess

                        }
                        }
                    });

这应该修复身份验证部分。

下一部分如何访问第二个firebase数据库??

访问辅助 Firebase 项目

FirebaseApp app = FirebaseApp.getInstance("secondary");
            FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);

            TestEntry testEntry = new TestEntry(orderId);

            DatabaseReference orderIdReference = secondaryDatabase.
                    getReference().child(orderId);

            Log.d("Secondary db", orderIdReference.toString());

            orderIdReference.setValue(testEntry);

来源:https://firebase.googleblog.com/2016/12/working-with-multiple-firebase-projects-in-an-android-app.html

【讨论】:

  • 非常感谢!我会尝试您的解决方案,并会尽快回复
  • 我已经编辑了我的答案,我已经添加了验证辅助 firebase 项目的代码。
猜你喜欢
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2019-04-27
  • 2020-02-24
  • 1970-01-01
相关资源
最近更新 更多