是的,一个项目可以有不同的 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