【问题标题】:Android Studio, Firebase Phone Authentication Opening A Browser?Android Studio,Firebase 电话身份验证打开浏览器?
【发布时间】:2020-11-11 11:20:12
【问题描述】:

我正在尝试使用 Firebase 在我的应用中添加电话身份验证,当我单击发送 OTP 按钮时,浏览器正在打开并显示验证您不是机器人并且只有在我通过选择图像验证我不是机器人之后,它正在发送 OTP。

而且 OTP 的格式也发生了变化。

以前的 OTP 格式是

435325 是您的 MyApplication 验证码。

OTP 的新格式是

789574 如果您的验证码为 myapplication.55b19.firebaseapp.com

我只是不明白为什么每次我请求 OTP 时都会打开浏览器

这是我的代码

public class MainActivity extends AppCompatActivity {

    EditText phoneEt,otpEt;
    Button loginBtn,verifyOTPBtn;
    private String verificationCodeBySystem;
    private FirebaseAuth firebaseAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        firebaseAuth = FirebaseAuth.getInstance();
        phoneEt = findViewById(R.id.phoneEt);
        otpEt = findViewById(R.id.otpEt);
        loginBtn = findViewById(R.id.loginBtn);
        verifyOTPBtn = findViewById(R.id.verifyOTPBtn);

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNumber = phoneEt.getText().toString().trim();
                sendOTPCodeToUser(phoneNumber);
            }
        });

        verifyOTPBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = otpEt.getText().toString().trim();
                if (code.isEmpty() || code.length() < 6){
                    otpEt.setError("Wrong OTP...");
                    otpEt.requestFocus();
                    return;
                }
                verifyCode(code);
            }
        });
    }


    private void sendOTPCodeToUser(String phoneNumber) {
        PhoneAuthOptions options =
                PhoneAuthOptions.newBuilder(firebaseAuth)
                        .setPhoneNumber("+91"+phoneNumber)       // Phone number to verify
                        .setTimeout(60L, TimeUnit.SECONDS) // Timeout and unit
                        .setActivity(this)                 // Activity (for callback binding)
                        .setCallbacks(mCallbacks)          // OnVerificationStateChangedCallbacks
                        .build();
        PhoneAuthProvider.verifyPhoneNumber(options);
    }

    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        @Override
        public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
            super.onCodeSent(s, forceResendingToken);
            verificationCodeBySystem = s;
        }

        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
            String code = phoneAuthCredential.getSmsCode();
            verifyCode(code);



        }

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

        }
    };

    private void verifyCode(String code) {

        try{
            PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationCodeBySystem,code);
            signInTheCredentials(credential);
        }catch (Exception e){

        }


    }

    private void signInTheCredentials(PhoneAuthCredential credential) {
        FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        firebaseAuth.signInWithCredential(credential)
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful()){
                        Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this, "Wrong OTP", Toast.LENGTH_SHORT).show();
                    }
                });
    }

}

Gradle 文件

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "package.myapplication"
        minSdkVersion 24
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-auth:20.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation platform('com.google.firebase:firebase-bom:26.0.0')
    implementation 'android.arch.persistence.room:runtime:1.1.1'
    implementation "androidx.browser:browser:1.2.0"

    annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'

}

【问题讨论】:

  • 嘿,你们解决了这个问题吗?

标签: android firebase authentication firebase-authentication


【解决方案1】:

你必须在你的谷歌云控制台中启用安全网才能按照Doc使用手机身份验证

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2020-09-19
    • 2020-01-24
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多