java - 当用户成功使用Google登录时,如何在FirebaseDatabase中获取用户信息?

标签 java android android-studio

当用户成功使用 Google 登录时,我想在我的 FirebaseDatabse 中获取用户信息,下面是我的代码:

@Override
 public void onActivityResult(int requestCode, int resultCode, Intent 
 data) {
 super.onActivityResult(requestCode, resultCode, data);
 // Result returned from launching the Intent from 
 GoogleSignInClient.getSignInIntent(...);
 if (requestCode == RC_SIGN_IN) {
 // The Task returned from this call is always completed, no need 
 to attach
 // a listener.
 Task<GoogleSignInAccount> task = 
 GoogleSignIn.getSignedInAccountFromIntent(data);
  try {
            GoogleSignInAccount acct = 
   task.getResult(ApiException.class);
      fireBaseAuthWithGoogle(acct);

      } catch (ApiException e) {
            Toast.makeText(this, "" + e.getMessage(), 
  Toast.LENGTH_SHORT).show();
        }
    }
  }

  private void fireBaseAuthWithGoogle(GoogleSignInAccount acct) {
  AuthCredential credential = 
  GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  mAuth.signInWithCredential(credential)
   .addOnCompleteListener(this, task -> {
     if (task.isSuccessful()) {
    FirebaseUser user = mAuth.getCurrentUser();                   
  DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid());
                    HashMap hashMap = new HashMap();
                    hashMap.put("username", user.getDisplayName());
                    hashMap.put("uid", user.getUid());
                    hashMap.put("profile", user.getPhotoUrl());

                    reference.push().setValue(reference);
                    //go to HomeActivity
                    startActivity(new 
              Intent(MainActivity.this,MainActivity2.class));

                } else {
                    // If sign in fails, display a message to the user.
                    Toast.makeText(MainActivity.this, "Login Failed...", 
     Toast.LENGTH_SHORT).show();
                }

            }).addOnFailureListener(e -> {
        //show error here
        Toast.makeText(MainActivity.this, "" + e.getMessage(), 
Toast.LENGTH_SHORT).show();
    });
 }
 }    

我想存储在实时数据库中,例如 用户 用户名: uid: 轮廓: 如果你教我如何做到这一点,我也会在其他人需要帮助时帮助他们:)

最佳答案

首先添加此实现“com.google.firebase:firebase-database:19.2.0” 到您的 gradle 文件

之后在您的 Activity 中

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
    // Name, email
    String name = user.getDisplayName();
    String email = user.getEmail();
DatabaseReference  usersref,idref,nameref,emailref;
          usersref = rootref.child("USERS");
         idref = users.child(id);
         nameref = idref.child("USER NAME");
         emailref = idref.child("EMAIL");
        nameref.setValue(name);
        emailref.setValue(email);
}

你的数据库几乎看起来像这样 Your database nearly looks likes this

关于java - 当用户成功使用Google登录时,如何在FirebaseDatabase中获取用户信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58404334/

相关文章:

java - 生成 Google map 发布 key

java - Heroku slug 大小计算?

java - 无法更新纬度和经度

java - 如何安排随机时间(每隔随机分钟)事件

android - 在android中显示具有轮播效果的图像

安卓工作室 “Button Tapped!”

java - 从 Android 设备调用 .Net 网络服务

java - Android webView不显示地理位置

java - Java 正则表达式 Kleene star 的奇怪问题

java - Selenium 无法仅在 Chrome 上定位 iframe 中的 div