我的应用程序使用Firebase身份验证与2个提供商:Facebook和Google。为了允许用户使用相同的电子邮件创建Facebook和Google帐户,我访问了Firebase控制台并选中了一个选项,允许多个帐户的电子邮件地址。
现在可行,但对于Facebook,电子邮件始终为空。如果用户决定使用Facebook登录,我如何获得顶级电子邮件地址(链接到多个帐户)。我不想将提供商链接到一个帐户,我想保持“允许一个邮件地址允许多个帐户”。
在这种情况下如何获取邮件地址的任何想法?
[编辑]
Google和Facebook apis的初始化:
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]
答案 0 :(得分: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();
}
}
}