1

我的应用使用 Firebase 身份验证与 2 个提供商:Facebook 和 Google。为了允许用户使用相同的电子邮件创建 Facebook 和 Google 帐户,我去了 Firebase 控制台并选中了一个选项以允许电子邮件地址使用多个帐户。

现在它可以工作了,但对于 Facebook,电子邮件始终为空。如果用户决定使用 Facebook 登录,我如何获得顶级电子邮件地址(链接到多个帐户)。我不想将提供商链接到一个帐户,我想保持“允许一个邮件地址使用多个帐户”打开。

在这种情况下如何获取邮件地址的任何想法?

[编辑]

Google 和 Facebook api 的初始化:

 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mAuth = FirebaseAuth.getInstance();

        mAuthListener = firebaseAuth ->
        {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null)
            {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                String uuid = user.getUid();
                String mail = user.getEmail();

                currentUserUid = user.getUid();

                //Toast.makeText(LoginActivity.this, "Zalogowano. Email: " + user.getEmail() + " uid: " + user.getUid(),
                //        Toast.LENGTH_SHORT).show();

                if (!logout)
                {
                    List<? extends UserInfo> provider = user.getProviderData();
                    AuthSocial(user, currentLoginType); //if "allow multiple accounts for mail enabled, user.getEmail always returns null for Facebook, how to get correct top level email for "multi account" here?
                }
                else
                {
                    //mAuth.signOut();
                }

            }
            else
            {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
        };

        // [START initialize_fblogin]
        // Initialize Facebook Login button


        mCallbackManager = CallbackManager.Factory.create();
        LoginButton loginButton = (LoginButton) findViewById(R.id.bt_go_facebook);
        loginButton.setReadPermissions("email", "public_profile");
        loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>()
        {


            @Override
            public void onSuccess(LoginResult loginResult)
            {
                Log.d(TAG, "facebook:onSuccess:" + loginResult);
                handleFacebookAccessToken(loginResult.getAccessToken());
                currentLoginType = "facebook";
            }

            @Override
            public void onCancel()
            {
                Log.d(TAG, "facebook:onCancel");
                // [START_EXCLUDE]
                //updateUI(null);
                // [END_EXCLUDE]
            }

            @Override
            public void onError(FacebookException error)
            {
                Log.d(TAG, "facebook:onError", error);
                // [START_EXCLUDE]
                //updateUI(null);
                // [END_EXCLUDE]
            }
        });
        // [END initialize_fblogin]
4

1 回答 1

3

因为 facebook 只允许用户使用电话号码登录,所以在某些情况下,用户不会使用他们的电子邮件地址更新他们的帐户。不幸的是,在这些情况下,您根本无法获得他们的电子邮件地址。要解决此问题,您可以使用提供的电话号码或唯一的Google/Facebook id.

为了从 facebook profie 获取电子邮件地址,您需要在对象上使用registerCallback方法。callbackManager这是代码:

CallbackManager callbackManager = CallbackManager.Factory.create();
facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        handleFacebookAccessToken(accessToken);

        GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                if(jsonObject != null){
                    Profile profile = Profile.getCurrentProfile();
                    String userEmail = jsonObject.optString("email"); // Facebook userEmail

                } 
            }
        });
        Bundle bundle = new Bundle();
        bundle.putString("fields", "email");
        request.setParameters(bundle);
        request.executeAsync();
    }

    @Override
    public void onCancel() {}

    @Override
    public void onError(FacebookException exception) {}
});

通过这种方式,您可以检查用户使用Google或登录的任何位置Facebook

if (firebaseUser != null) {
    for (UserInfo userInfo : firebaseUser.getProviderData()) {
        if (userInfo.getProviderId().equals("facebook.com")) {
            Toast.makeText(MainActivity.this, "User is signed in with Facebook", Toast.LENGTH_SHORT).show();
        }

        if (userInfo.getProviderId().equals("google.com")) {
            createShoppingList(userModel, listName);
            Toast.makeText(MainActivity.this, "You are signed in Google!", Toast.LENGTH_SHORT).show();
        }
    }
}
于 2017-04-18T11:23:02.880 回答