Android应用Google登录Firebase认证成功却未写入实时数据库
Firebase Google登录成功但用户数据无法写入Realtime Database排查方案
问题概述
开发Android应用时,Google登录认证流程可正常完成,日志能输出正确的用户信息,但用户数据未按预期写入Firebase Realtime Database。已配置数据库规则允许已认证用户进行读写操作。
当前配置
Realtime Database规则
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }
App级Build Gradle配置
plugins { id 'com.android.application' id 'com.google.gms.google-services' } android { namespace 'com.arsoftltd.lolipop' compileSdk 34 defaultConfig { applicationId "com.arsoftltd.lolipop" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } buildFeatures{ dataBinding true } } dependencies { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.10.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' // Import the Firebase BoM implementation platform('com.google.firebase:firebase-bom:32.6.0') // When using the BoM, don't specify versions in Firebase dependencies implementation 'com.google.firebase:firebase-analytics' // Firebase Authentication implementation 'com.google.firebase:firebase-auth' implementation "com.google.android.gms:play-services-auth:20.7.0" implementation 'com.firebaseui:firebase-ui-auth:8.0.2' // Firebase realtime database implementation "com.google.firebase:firebase-database" // Facebook Sdk implementation 'com.facebook.android:facebook-android-sdk:[8,9)' // SSP SDP library implementation 'com.intuit.ssp:ssp-android:1.1.0' implementation 'com.intuit.sdp:sdp-android:1.1.0' }
核心代码片段
public class SignInActivity extends AppCompatActivity { private FirebaseAuth auth = FirebaseAuth.getInstance(); private FirebaseDatabase database = FirebaseDatabase.getInstance(); private int RC_SIGN_IN = 20; // ... 其他初始化代码省略 private void firebaseAuth(String idToken) { AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null); auth.signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { FirebaseUser user = auth.getCurrentUser(); Map<String, Object> map = new HashMap<>(); map.put("id", user.getUid()); map.put("name", user.getDisplayName()); String photoUrl = (user.getPhotoUrl() != null) ? user.getPhotoUrl().toString() : ""; map.put("profile", photoUrl); Log.d("FirebaseGoogleAuth", "id" + user.getUid() + " " + user.getDisplayName() + " " + photoUrl); database.getReference() .child("users") .child(user.getUid()) .setValue(map) .addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Log.d("FirebaseGoogleAuth", "Data write successful"); Intent intent = new Intent(getApplicationContext(), FeedActivity.class); startActivity(intent); finish(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.d("FirebaseGoogleAuth", "error realtime database " + e.getMessage().toString()); } }); } else { Toast.makeText(SignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); } } }); } @Override protected void onStart() { super.onStart(); FirebaseUser currentUser = auth.getCurrentUser(); if (currentUser != null){ startActivity(new Intent(SignInActivity.this, FeedActivity.class)); finish(); } } }
认证日志信息
2023-11-27 17:17:59.062 28378-28378 FirebaseGoogleAuth com.arsoftltd.lolipop D idpuhGxZmHw7YfW49R4ZGfHJsLmus2 Md Abu Rayhan https://lh3.googleusercontent.com/a/ACg8ocIujr5T5CNmjjEps2SKElmg5ROz0Qbdl0cNcfPAUwcnvQ=s96-c
排查思路与建议
- 确认数据库实例指向:检查
FirebaseDatabase.getInstance()是否连接到正确的Realtime Database实例,多项目或非默认区域需显式指定URL:FirebaseDatabase.getInstance("https://your-db-url.firebaseio.com/")。 - 查看写入失败日志:重点关注Logcat中
error realtime database开头的日志,直接定位权限、网络或数据格式问题。 - 验证认证会话有效性:写入前调用
auth.getCurrentUser().getIdToken(true)刷新令牌,确保用户认证状态有效。 - 检查数据库区域配置:若数据库使用非默认区域,需在AndroidManifest.xml添加对应meta-data,或初始化时指定区域,避免连接失败。
- 确认网络状态:测试设备需有稳定网络连接,离线持久化数据需联网后才会同步到云端。
- 核对数据路径:检查Firebase控制台中数据库路径是否为
users/{uid},确认无其他操作意外覆盖数据。 - 临时测试规则:测试环境下可临时将规则设为
".write": true,排除规则配置问题。
内容的提问来源于stack exchange,提问作者Md Abu Rayhan
相关产品推荐
相关产品推荐

