SMS 验证码请求失败,状态码未知 17006,同时在 android 上使用 firebase phone-auth api 进行身份验证

时间:2021-02-02 18:01:58

标签: android firebase firebase-authentication

我不知道这个错误是什么意思。我在 Google 上搜索了很多,但找不到为什么会出现此错误。

E/FirebaseAuth: [SmsRetrieverHelper] SMS verification code request failed: unknown status code: 17006 null

我正在尝试 authenticate android client with firebase phone auth sign-in method,并尝试通过 sending a verification code on their phone 验证他们的电话号码。没有发送代码,也没有调用回调函数。我不知道这里有什么问题

这是从edittext获取电话号码的活动,并在该电话号码上发送验证码,并开始下一个活动,用户将输入他们获得的代码:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.FirebaseException;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class PhoneOtpAuth extends AppCompatActivity {

    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_otp_auth);

        EditText phoneNumberEditText = findViewById(R.id.phone_number);
        Button signIn = findViewById(R.id.sign_in_button);

        mAuth = FirebaseAuth.getInstance();

        signIn.setOnClickListener(v -> {
            String phoneNumber = phoneNumberEditText.getText().toString();

            PhoneAuthOptions options =
                    PhoneAuthOptions.newBuilder(mAuth)
                            .setPhoneNumber(phoneNumber)
                            .setTimeout(60L, TimeUnit.SECONDS)
                            .setActivity(PhoneOtpAuth.this)
                            .setCallbacks(callbacks)
                            .build();

            PhoneAuthProvider.verifyPhoneNumber(options);
        });
    }

    PhoneAuthProvider.OnVerificationStateChangedCallbacks callbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
            Intent signUpIntent = new Intent(PhoneOtpAuth.this, DriverSignup.class);
            startActivity(signUpIntent);
            finish();
        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {

        }

        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {    
            Intent otpVerificationIntent = new Intent(PhoneOtpAuth.this, OtpVerificationActivity.class);
            otpVerificationIntent.putExtra("VerificationId", s);
            startActivity(otpVerificationIntent);
            finish();
        }
    };
}

这是验证用户从上一个(以上)活动中获得的验证码的活动:

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthOptions;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class OtpVerificationActivity extends AppCompatActivity {

    private String otp;

    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp_verification);

        EditText otpEditText = findViewById(R.id.otp);
        Button verifyOtp = findViewById(R.id.verify_otp_button);

        mAuth = FirebaseAuth.getInstance();

        otp = otpEditText.getText().toString();
        String verificationId = getIntent().getExtras().getString("VerificationId");
        FirebaseAuth mAuth = getIntent().getExtras().getParcelable("mAuth");

        verifyOtp.setOnClickListener(v -> {
            PhoneAuthCredential phoneAuthCredential = PhoneAuthProvider.getCredential(verificationId, otp);

            mAuth.signInWithCredential(phoneAuthCredential)
                    .addOnCompleteListener(task -> {
                        if(task.isSuccessful()) {
                            Intent signUpIntent = new Intent(OtpVerificationActivity.this, DriverSignup.class);
                            startActivity(signUpIntent);
                            finish();
                        }
                    });
        });
    }
}

3 个答案:

答案 0 :(得分:1)

好吧,我想您可能忘记在 Firebase 控制台中启用电话登录方法

尝试将手机添加为登录提供商,然后重试。

答案 1 :(得分:1)

当您重复使用特定的电话号码,为了测试而登录和退出,而没有将其添加到“测试电话号码”中时,Google 最终可能会暂时阻止它,并显示错误指示已检测到异常行为,或者出现错误:“短信验证码请求失败:未知状态码:17010 null”。

答案 2 :(得分:0)

确保设备已连接到互联网。 同样在 Google Cloud Console 中,为您的项目启用 Android DeviceCheck API。这样,firebase SDK 就可以验证设备以继续进行身份验证。