【问题标题】:Firebase Auth Error While Trying To Create An Account尝试创建帐户时出现 Firebase 身份验证错误
【发布时间】:2020-11-03 15:33:50
【问题描述】:

自 8 月以来我没有接触过这段代码,它工作正常,但自从我昨天重新开始使用它后,我一直收到这个错误:

带标签:createUserWithEmail:failure

com.google.firebase.FirebaseException:发生内部错误。 [无法解析主机“www.googleapis.com”:没有与主机名关联的地址]

我的所有依赖项都已正确设置。我检查了无数次,并在 YouTube 上花费了数小时,重新下载了 json 文件,更新了 Android Studio 和我的模拟器。这是我最后的手段。我希望有人能帮帮忙。这是我的代码

        mAuth.createUserWithEmailAndPassword(email, password)
            .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, "createUserWithEmail:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        Toast.makeText(Register.this, "Sign up success! Taking you to login...",
                                Toast.LENGTH_SHORT).show();
                        
                         
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(Register.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    
                    }
      }
            })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.d(TAG, e.getMessage());
            Log.d(TAG, "FAIL");
        }
    });

【问题讨论】:

  • 你的模拟器有网络吗?检查右上角的图标
  • 您有正确的安全规则吗?
  • 在没有 Internet 连接时出现此错误。是的,这样的错误有歧义,它可能是一个firebase错误,阻止了auth api清楚地发出“无连接错误”。

标签: java android firebase firebase-authentication


【解决方案1】:

我上次使用 firebase auth 时使用了这段代码:

AuthUI.IdpConfig googleIdp = new AuthUI.IdpConfig.EmailBuilder()
                        .build();

                startActivityForResult(AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setIsSmartLockEnabled(false)
                        .setTosAndPrivacyPolicyUrls("Política de privacidad", "http://twitter.com")
                        .setAvailableProviders(Arrays.asList(new AuthUI.IdpConfig.EmailBuilder().build(),
                                facebookIdp, googleIdp))
                        .setTheme(R.style.GreenTheme)
                        .setLogo(R.drawable.img_multi_login)
                        .build(), RC_SIGN_IN);

结果:

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        if (resultCode == RESULT_OK) {
            Toast.makeText(this, "Welcome", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_SHORT).show();
        }
    } 
}

并使用 AuthStateListener 来更改用户的数据,如果发生更改或用户是否已登录。

我认为这是在 Android 上实现 firebase auth 的最简单方法。

【讨论】:

    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 2016-11-07
    • 2016-10-15
    • 1970-01-01
    相关资源
    最近更新 更多