我正在尝试使用Firebase数据库系统在我的android项目中添加电话身份验证,但是我遇到了“ PhoneAuth Cardential”问题。它显示电话身份验证凭证无法解析,并且无法从外部软件包访问电话身份验证卡。我想使用用户输入的联系电话上发送的代码对用户进行身份验证,以便在身份验证后允许用户登录。>
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rengwuxian.materialedittext.MaterialEditText;
import java.util.concurrent.TimeUnit;
public class Log_in extends AppCompatActivity {
private MaterialEditText phoneEditText;
private EditText editText;
private Button continueButton, sendCode;
private TextView mainTextView,miniTextVeiw;
private String phoneNumber;
private boolean phoneAndCode=false;
private String verificationID;
private PhoneAuthProvider.ForceResendingToken resendToken;
private FirebaseAuth firebaseAuth;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacksInstance;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_in);
phoneEditText = (MaterialEditText) findViewById(R.id.phoneEditText);
continueButton = (Button) findViewById(R.id.continueButton);
editText = (EditText) findViewById(R.id.editText);
sendCode = (Button) findViewById(R.id.code);
mainTextView=findViewById(R.id.textView2);
miniTextVeiw=findViewById(R.id.textView3);
firebaseAuth = FirebaseAuth.getInstance();
sendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public void onContinueClick(View v) {
if(phoneAndCode==false)
{
if (phoneEditText.getText().toString() != null && phoneEditText.getText().toString().length() == 11) {
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
phoneNumber = phoneEditText.getText().toString();
phoneNumber="+92"+phoneNumber.substring(1);
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("please wait");
progressDialog.setMessage("sending code");
progressDialog.setCancelable(false);
// phoneVerifier();
if (MainActivity.flag == 1) {
databaseReference.child("Driver").child(phoneNumber).setValue(null);
phoneVerifier();
} else if (MainActivity.flag == 0) {
// Toast.makeText(this,"called for driver and phone number is"+phoneNumber,Toast.LENGTH_SHORT).show();
databaseReference.child("Passenger").child(phoneNumber).setValue(null);
phoneVerifier();
}
phoneAndCode=true;
} else {
phoneEditText.setError("invalid phone number");
}
}
else if(phoneAndCode==true)
{
String verificationCode = null;
PhoneAuthCredential credential = null;
if (!(phoneEditText.getText().toString().equals(null))) {
verificationCode = phoneEditText.getText().toString();
credential = new PhoneAuthCredential(verificationID, verificationCode);
signInWithPhoneAuthCredential(credential);
} else
phoneEditText.setError("invalid verification code");
}
}
private void phoneVerifier() {
progressDialog.show();
callbacksInstance = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Snackbar.make(findViewById(R.id.welcomeRoot), "invalid phone number", Snackbar.LENGTH_SHORT).show();
//if the phone number is wrong;
}
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
// The SMS verification code has been sent to the provided phone number, we
// now need to ask the user to enter the code and then construct a credential
// by combining the code with a verification ID.
Snackbar.make(findViewById(R.id.welcomeRoot), "code sent", Snackbar.LENGTH_SHORT).show();
// Save verification ID and resending token so we can use them later
verificationID = verificationId;
resendToken = token;
progressDialog.dismiss();
// continueButton.setVisibility(View.INVISIBLE);
// sendCode.setVisibility(View.VISIBLE);
continueButton.setText("Send verification code");
mainTextView.setText("Enter verification code that you have recently received");
miniTextVeiw.setText("Enter verification code");
phoneEditText.setHint("enter verification code");
phoneEditText.setText(null);
// ...
}
};
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
callbacksInstance); // OnVerificationStateChangedCallbacks
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
firebaseAuth.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
FirebaseUser user = task.getResult().getUser();
if (MainActivity.flag == 0) {
startActivity(new Intent(Log_in.this, DriverMapsActivity.class));
finish();
} else if (MainActivity.flag == 1) {
startActivity(new Intent(Log_in.this, CustomerMapsActivity.class));
finish();
}
// ...
} else {
// Sign in failed, display a message and update the UI
Toast.makeText(Log_in.this, "sign in failed try again", Toast.LENGTH_SHORT).show();
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(Log_in.this, "You have enterd an INVALID CODE", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
答案 0 :(得分:2)
您应该尝试此行
credential =PhoneAuthProvider.getCredential(verificationID,verificationCode);
代替
credential = new PhoneAuthCredential(verificationID, verificationCode);