簡體   English   中英

Firebase google authentication getCurrentUser.getEmail() 返回 null,但它的返回值為 getCurrentUser.getDisplayName()

[英]Firebase google authentication getCurrentUser.getEmail() returns null, but its returning value for getCurrentUser.getDisplayName()

我使用 firebase 創建了一個 google 登錄,然后是 firebase 的官方文檔。

https://github.com/firebase/snippets-android/blob/7127256e50fffc2a34db02986dc629443b6f56d1/auth/app/src/main/java/com/google/firebase/quickstart/auth/GoogleSignInActivity.java#L67-L68

它以前工作正常。 現在它為 getCurrentUser.getEmail() 返回一個空值。 但它正在返回其他帳戶詳細信息的值,例如用戶 ID、顯示名稱等,如下所示。

在此處輸入圖像描述

public class UserLogin extends AppCompatActivity implements View.OnClickListener {

    DatabaseHelper user_tbl;
    private ImageView gmail;
    String TAG = "UserLoginActivity";
    private static final int RC_SIGN_IN = 9001;

    // [START declare_auth]
    private FirebaseAuth mAuth;
    // [END declare_auth]
    private GoogleSignInClient mGoogleSignInClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        user_tbl= new DatabaseHelper(this);

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken("284368548772-2gr7d5vqibjcd4jf08jsuhaquk21rnlu.apps.googleusercontent.com")
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
        // [END config_signin]

        // [START initialize_auth]
        // Initialize Firebase Auth
        mAuth = FirebaseAuth.getInstance();
        setId();
        setEvents();
    }
   
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "----------------------------onActivityResult---------------------------");
        super.onActivityResult(requestCode, resultCode, data);
        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());
            } catch (ApiException e) {
                // Google Sign In failed, update UI appropriately
                Log.w(TAG, "Google sign in failed", e);
            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {
        Log.d(TAG, "------------------------firebaseAuthWithGoogle-----------------------");
        AuthCredential credential = GoogleAuthProvider.getCredential(idToken, null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            String gmail_user_email = user.getEmail();
                            Log.d(TAG, "gmail_user_email"+gmail_user_email);
                            String gmail_user_name = user.getDisplayName();
                            Log.d(TAG, "gmail_user_name"+gmail_user_name);
                            String gmail_user_phone = user.getPhoneNumber();
                            Log.d(TAG, "gmail_user_phone"+gmail_user_phone);
                            String gmail_user_id = user.getUid();
                            Log.d(TAG, "gmail_user_id"+gmail_user_id);
                            new Display_Snackbar(UserLogin.this).Show_snack_bar("Sign in as "+gmail_user_email);
                            int checkUser = user_tbl.CheckUser(gmail_user_email);
                            if(checkUser>0) {
                                Log.d(TAG, "user id exist in sqlite");
                                Boolean insert_login_details = user_tbl.InsertLoginDetails(gmail_user_email,"1");
                                syncData(gmail_user_email);
                                getDevices(gmail_user_email);
                            }else{
                                Log.d(TAG, "user id not exist in sqlite");
                                InsertNewUser(gmail_user_email,"",gmail_user_name,"");
                            }
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            new Display_Snackbar(UserLogin.this).Show_snack_bar("Gmail Sign in failed");
                        }
                    }
                });
    }
  
    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

如果是社交登錄,您將獲得 id(social Token)

有2分

  1. 如果用戶有權讀取數據,那么您將獲得像 facebook 一樣的數據,我們可以將個人資料數據私有化

  2. 在您的情況下,您沒有在憑據中發送數據,您只是在這種情況下傳遞令牌,firebase 不保存電子郵件,這將僅返回帶有 subId 的用戶。

     GoogleAuthProvider.getCredential(idToken, null)

在這里您可以獲取電子郵件並傳遞給憑據

GoogleSignInAccount account = task.getResult(ApiException.class);
Log.d(TAG, "firebaseAuthWithGoogle:" + account.getEmail());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM