无法通过Firebase获取用户的Gmail电子邮件地址 [英] Unable to fetch user's gmail email address through Firebase

查看:140
本文介绍了无法通过Firebase获取用户的Gmail电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款使用Firebase作为BaaS的应用程序。

我已经集成了Google Sign-in,并且想要获取用户名,电子邮件地址和个人资料照片。



我已成功提取姓名和个人资料照片,但无法取得电子邮件地址。



这里是Google API客户端设置:

  / *设置Google API对象以允许Google+登录* / 
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();

以下是我提取用户数据的操作:
$ b $ ($ authData!= null){
if(authData.getProvider()。equals( google)){

googleName =(String)authData.getProviderData()。get(displayName);
googleEmail =(String)authData.getProviderData()。get(email);
googleImage =(String)authData.getProviderData()。get(profileImageURL);

$ b} else {
Toast.makeText(getBaseContext(),error,Toast.LENGTH_LONG).show();






这段代码已经成功提取了名字和个人资料图片,但它正在返回 null 作为电子邮件地址。



请让我知道这里出了什么问题。

解决方案

不知道这是否是您的情况,但是您可以获取其他字段但getEmail返回null的一种情况是在Firebase控制台>身份验证>登录方法您允许使用相同的电子邮件地址创建多个帐户



有一个



您必须使用以下选项:防止使用相同的电子邮件创建多个帐户地址,否则 FirebaseUser.getEmail()将返回null



ps:只有在该选项关闭后首次登录的用户才会能够成功使用这个方法

I'm developing an app which use Firebase` as a BaaS.

I have integrated Google Sign-in and I want to fetch user's name, email address and profile pic.

I have successfully fetched the name and profile pic, but I'm unable to fetch the email address.

Here's Google API client setup:

/* Setup the Google API object to allow Google+ logins */
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Plus.API)
                .addScope(Plus.SCOPE_PLUS_LOGIN)
                .build();

Here's what I have done to fetch user's data:

public void fetchDataFromGoogle() {

        if (authData != null) {
            if (authData.getProvider().equals("google")) {

                googleName = (String) authData.getProviderData().get("displayName");
                googleEmail = (String) authData.getProviderData().get("email");
                googleImage = (String) authData.getProviderData().get("profileImageURL");

            }
        } else {
            Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show();
        }
    }

This code have successfully fetched the name and profile pic, but it is returning null for the email address.

Please let me know what's going wrong here.

解决方案

Not sure if it is your case, but one situation where you are able to get other fields but getEmail returns null is when in Firebase console > Authentication > Sign-In Methods you allow "creation of multiple accounts with the same email address"

There's a closed bug report already filed with google:

When you go to the Auth > Sign-In Methods page of your project in the Firebase console. do you have One account per email address on or off? If you allow multiple accounts per email address, you will get null for FirebaseUser.getEmail()

You have to use the option: "Prevent creation of multiple accounts with the same email address" or else FirebaseUser.getEmail() will return null

Ps: only users who first logged in after the option was turned off will be able to use this method successfully

这篇关于无法通过Firebase获取用户的Gmail电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆