firebaseUser.getEmail() 使用 Firebase Auth UI 返回 null

Khe*_*raj 5 android firebase firebase-authentication

问题:

我面临一个问题。我在我的 Android 应用程序中使用Firebase Auth UI。我已在 GoogleSignInOptions 中请求提供电子邮件。

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.web_client_id))
            .requestEmail()
            .build();
Run Code Online (Sandbox Code Playgroud)

在登录构建器中传递此 GoogleSignInOptions

            new AuthUI.IdpConfig.GoogleBuilder()
                    .setSignInOptions(gso)
                    .build(),
Run Code Online (Sandbox Code Playgroud)

现在,当我在应用程序中通过谷歌登录时,onActivityResult()被调用,然后我无法通过 获取电子邮件FirebaseUser.getEmail()。同时IdpResponse包含电子邮件。

IdpResponse response = IdpResponse.fromResultIntent(data); // response.getEmail() returns email
Run Code Online (Sandbox Code Playgroud)

上面IdpResponse包含电子邮件。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); // user.getEmail() is null
Run Code Online (Sandbox Code Playgroud)

以上FirebaseUser对象不包含电子邮件。

目的

我希望 FirebaseUser 对象中有电子邮件。因为还有其他登录提供商。如 Facebook、电子邮件/通行证、Twitter。所以我正在寻找一个独特的解决方案。

小智 4

在此处查看文档文档链接

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

if (user != null) {
    for (UserInfo profile : user.getProviderData()) {
        // Id of the provider (ex: google.com)
        String providerId = profile.getProviderId();

        // UID specific to the provider
        String uid = profile.getUid();

        // Name, email address, and profile photo Url
        String name = profile.getDisplayName();
        String email = profile.getEmail();
        Uri photoUrl = profile.getPhotoUrl();
Toast.makeText(Activity.this, email, Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)