您好,我想知道如何在我的 android 应用程序中通过 Github 对用户进行身份验证时访问用户电子邮件。我正在使用 firebase 和 android studio。请注意,我无法使用user.getEmail
in,onSuccesslistner
因为我也在使用 Google 身份验证,这会引发电子邮件已存在且待处理任务方法仅第一次有效的异常。基本上我想用来setScopes
检索用户电子邮件。我必须获取电子邮件并检查用户是否存在于我的数据库中,只需登录用户即可。
这是我的代码:
public void git_login(View view)
{
SignInWithGithubProvider(
OAuthProvider.newBuilder("github.com")
.setScopes(
new ArrayList<String>()
{
{
add("user:email");
}
}).build()
);
}
private void SignInWithGithubProvider(OAuthProvider login)
{
Task<AuthResult> pendingResultTask= mAuth.getPendingAuthResult();
if (pendingResultTask!=null)
{
// There's something already here! Finish the sign-in for your user.
pendingResultTask
.addOnSuccessListener(
new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
Toast.makeText(getApplicationContext(), "User Exist" + authResult, Toast.LENGTH_SHORT).show();
// User is signed in.
// IdP data available in
// authResult.getAdditionalUserInfo().getProfile().
// The OAuth access token can also be retrieved:
// authResult.getCredential().getAccessToken().
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Handle failure.
}
});
}
else {
// There's no pending result so you need to start the sign-in flow.
// See below.
mAuth.startActivityForSignInWithProvider(this , login).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
if (e.toString().equals("com.google.firebase.auth.FirebaseAuthUserCollisionException: An account already exists with the same email address but different sign-in credentials. Sign in using a provider associated with this email address."))
{
showDialogAlert();
}
}
}).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(getApplicationContext(), "Login" + user.getUid() +"\n"+user.getEmail(), Toast.LENGTH_SHORT).show();
userNameForDb = user.getDisplayName();
userImageForDb = String.valueOf(user.getPhotoUrl());
userEmailForDb = user.getEmail();
Toast.makeText(CreateNewAccountActivity.this, "Account added to Firebase: " +userNameForDb+"\n"+userEmailForDb+"\n"+userTokenForDb, Toast.LENGTH_SHORT).show();
saveDataToDb(userNameForDb , userEmailForDb , userTokenForDb);
}
});
}
}
```