java - Firebase FirebaseAuth.getInstance().getCurrentUser().getDisplayName() 返回特定 Gmail 地址的 null

标签 java android firebase firebase-authentication

从标题来看,您可能会认为我的问题有一个已发布的解决方案,但实际上我相信我阅读了与我的问题相关的所有内容,但没有一个完全匹配。这是我得到的:

首先,这个问题在今天之前在我的应用中从未发生过,我的登录也没有发生任何变化:相同的 Google 登录代码、相同的帐户。只要我一直在测试,过去三个月一切都一样。而且,据我所知,该问题仅发生在单个帐户上。当用户开始登录过程时,他们会看到一个用于登录的帐户选择;在本例中,我选择了第一个用户:

enter image description here

接下来,Google 对用户进行身份验证,我们在代码中到达这里:

enter image description here

所以问题是,为什么 Firebase 突然停止提供显示名称(和照片网址)?从第一个屏幕截图可以清楚地看出,该用户具有指定的名称和照片。除此之外,FirebaseAuth.getInstance().getCurrentUser().isAnonymous() 正如预期的那样是 false。任何关于为什么会突然崩溃的想法将不胜感激!

最佳答案

我通过简单地使用 UserProfileChangeRequest() 更新我的姓名和照片网址以及我的 Google 帐户的姓名和照片网址解决了该问题,如建议 here :

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    sendFirebaseEvent(FIREBASE_GOOGLE_AUTH_EVENT, FIREBASE_GOOGLE_AUTH_KEY, acct.getId());
    // [START_EXCLUDE silent]
    showProgressDialog();
    googleAccount = acct;
    // [END_EXCLUDE]
    final AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        sendFirebaseEvent(FIREBASE_GOOGLE_AUTH_EVENT, FIREBASE_GOOGLE_AUTH_RESULT_KEY, "Success");
                        launchNextActivity();
                    } else {
                        // If sign in fails, display a message to the user.
                        sendFirebaseEvent(FIREBASE_GOOGLE_AUTH_EVENT, FIREBASE_GOOGLE_AUTH_RESULT_KEY, task.getException().getMessage());
                        Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
                        updateUI(null);
                    }

                    // [START_EXCLUDE]
                    hideProgressDialog();
                    // [END_EXCLUDE]
                }
            });
}
private void launchNextActivity() {
    mFirebaseAuth = FirebaseAuth.getInstance();
    FirebaseUser mUser = mFirebaseAuth.getCurrentUser();
    if (mUser.getDisplayName() == null || mUser.getDisplayName().length() == 0) {
        UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                .setDisplayName(googleAccount.getDisplayName())
                .setPhotoUri(googleAccount.getPhotoUrl())
                .build();
        mUser.updateProfile(profileUpdates);
    }
}

关于java - Firebase FirebaseAuth.getInstance().getCurrentUser().getDisplayName() 返回特定 Gmail 地址的 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60155835/

相关文章:

android - GoogleAuthUtil.getToken() 抛出 : RuntimeException: Can't create handler inside thread that has not called Looper. prepare()

android - 在 Kotlin 中处理推送通知

java - DDD 中的存储库层

java - 用于解析 csv 中的文件扩展名的正则表达式

java - 按住按钮的同时做某事

java - Android 应用程序 Activity

ios - FUIEmailAuth 未声明

swift - Firebase (3.2.0) 使用 Swift (2.0) 查询的确切方式是什么?

java - 根据对象类型的数组列表搜索特定输入

java - java新手,想要创建一个循环来生成对象