【问题标题】:Unable to authenticate with Firebase无法通过 Firebase 进行身份验证
【发布时间】:2018-08-07 16:28:31
【问题描述】:

我正在尝试使用通过控制台添加的凭据在模拟器上登录 Firebase。

代码如下:

val auth = FirebaseAuth.getInstance()

auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // ...
            } else {
                // ...
            }
        }

OnCompleteListener 回调永远不会被触发。

我已遵循 Firebase 文档中的指南

  • google-services json 文件
  • 项目依赖关系
  • 应用依赖项
  • 启用电子邮件/密码登录方法

知道我做错了什么吗?

Gradle 文件 - 项目:

buildscript {
    ext.kotlin_version = '1.2.60'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        classpath 'com.google.gms:google-services:4.0.2'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Gradle 文件 - 应用程序

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "..."
        minSdkVersion 23
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner  "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'

    implementation 'com.google.firebase:firebase-auth:16.0.2'
    implementation 'com.google.firebase:firebase-database:16.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.1'
}

apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 请分享 gradle 文件@345
  • 你把 SHA 密钥放到 firebase 里了吗?
  • @Raj 我已经分享了 gradle 文件。
  • 是的,SHA-1 密钥已设置。
  • @345 您是否尝试在if (task.isSuccessful) 语句的else 部分中记录task.getException(),以实际查看问题所在?

标签: android firebase kotlin firebase-authentication


【解决方案1】:

您的电子邮件或密码有效吗?

您可以添加“addOnFailureListener”以查看实际发生的情况。

 auth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // ...
                } else {
                    // ...
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(context, e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            });

【讨论】:

  • 凭据有效。 onFailure 也没有触发。
  • 或者您必须将您的 firebase-auth 依赖项更新到较新的版本。现在是 16.0.2
  • 我已更新到最新的依赖项,但问题仍然存在。
  • 您是否添加了互联网权限?
  • 你可以尝试在你的项目 build.gradle 中的 allprojects -> repositories 中添加 maven { url 'maven.google.com' },然后再次清理/重建
【解决方案2】:

尝试在build.gradle文件中添加如下依赖:-

implementation 'com.google.firebase:firebase-core:16.0.1'

我还建议您使用最新的 firebase 依赖项。

【讨论】:

  • 我已经添加了核心依赖并更新了其他的,但问题仍然存在。
  • 还将 google 服务更新为类路径 'com.google.gms:google-services:4.0.2'
  • 我做了但仍然没有成功:(
【解决方案3】:

我解决了这个问题

  • 添加新模拟器
  • 在开发者控制台中启用 Identity Toolkit API(错误仅在新模拟器上的 logcat 中可见)

此解决方案可能对使用模拟器并尝试过所有建议答案的人有所帮助...

【讨论】:

    猜你喜欢
    • 2019-10-05
    • 2023-04-01
    • 2012-04-01
    • 2014-01-05
    • 1970-01-01
    • 2018-04-12
    • 2019-08-09
    • 2018-12-22
    • 2015-09-15
    相关资源
    最近更新 更多