![](/img/trans.png)
[英]Flutter integration test firebase auth (email link or google sign in)
[英]Firebase Auth - no email from google sign in provider
使用 Google 提供商登录时电子邮件为空
我已经尝试使用 3 个 Gmail 帐户 - 其中 1 个没问题,但是使用 2 个我得到空。 这三个都正确地列在控制台中,帐户的电子邮件作为“标识符”
currentUser = FirebaseAuth.getInstance().getCurrentUser();
String Email = currentUser.getEmail();
代码创建授权:
providers = Arrays.asList(
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.FacebookBuilder().build()
);
startActivityForResult(
AuthUI.getInstance().createSignInIntentBuilder()
.setAvailableProviders(providers)
.setIsSmartLockEnabled(false)
.build(),MY_REQUEST_CODE);
我遇到了同样的问题,没有返回 Google 电子邮件地址。
添加范围电子邮件后:
var provider = new this.$firebase.auth.GoogleAuthProvider();
provider.addScope('email');
它正在返回 providerdata 中的电子邮件地址。
我在 Facebook 登录时也遇到了同样的问题。 我需要像这样添加范围
var provider = new this.$firebase.auth.FacebookAuthProvider();
provider.addScope("email");
试试这个,我有同样的问题,但在应用GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(LoginActivity.this);
使用 firebase 进行身份验证后,问题得到解决。
private void firebaseAuthWithGoogle(String idToken) {
AuthCredential credential = GoogleAuthProvider.getCredential(idToken, 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
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
String a="Email: "+user.getEmail()
+"\nName: "+user.getDisplayName()+"\nL= "+user.getProviderData();
//Here I got email field as null: 1 of them is OK, but with remaining I get null
Log.d(TAG, "onComplete: USER DATA:\n"+a);
// Add this code to get current sign-in user info
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(LoginActivity.this);
if (acct != null) {
String personName = acct.getDisplayName();
String personGivenName = acct.getGivenName();
String personFamilyName = acct.getFamilyName();
String personEmail = acct.getEmail();
String personId = acct.getId();
Uri personPhoto = acct.getPhotoUrl();
Log.d(TAG, "onComplete: RESULT--> "+"Name: "+personName+"\n"+
"Email: "+personEmail+"\n"+
"ID: "+personId+"\n"+
"Photo: "+personPhoto+"\n"+
"PersonGiven Name: "+personGivenName+"\n");
}
// updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Snackbar.make(r, "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
// updateUI(null);
}
// ...
}
});
}
在我的代码中,我可以看到 <currentUser.providerData> 包含两个列表。 经过比较,第0个列表没有email值,但是包含了displayName和photoUrl。 第一个列表包含所有三个值。 所以我使用第一个值。 但我不知道确切的原因。
我将测试项目中的代码复制到我的主项目中,问题解决了。 我不明白为什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.