Firebase身份验证错误-迁移项目后

时间:2019-07-07 19:04:04

标签: firebase kotlin firebase-authentication google-oauth google-oauth2

预期

在IntelliJ中运行的Kotlin应用程序已从2009 MacBook Pro迁移到2019 MacBook Pro。提供相同的凭据,该项目有望运行相同的代码并访问 Firestore数据库 Firestore存储

已观察

由于 oauth 错误,该代码将无法在新计算机上运行。

错误

  1.   

    com.google.api.gax.grpc.InstantiatingGrpcChannelProvider未定义或继承已解析方法抽象的NeedsCredentials()的实现

  2.   

    无法检测我们是否在Google Compute Engine上运行。

日志

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jul 07, 2019 11:45:41 AM com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
INFO: Failed to detect whether we are running on Google Compute Engine.
Exception in thread "Timer-0" java.lang.AbstractMethodError: Receiver class com.google.api.gax.grpc.InstantiatingGrpcChannelProvider does not define or inherit an implementation of the resolved method abstract needsCredentials()Z of interface com.google.api.gax.rpc.TransportChannelProvider.
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:157)
at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:122)
at com.google.cloud.firestore.spi.v1beta1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:121)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreRpcFactory.create(FirestoreOptions.java:80)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreRpcFactory.create(FirestoreOptions.java:72)
at com.google.cloud.ServiceOptions.getRpc(ServiceOptions.java:510)
at com.google.cloud.firestore.FirestoreOptions.getFirestoreRpc(FirestoreOptions.java:315)
at com.google.cloud.firestore.FirestoreImpl.<init>(FirestoreImpl.java:76)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreFactory.create(FirestoreOptions.java:63)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreFactory.create(FirestoreOptions.java:56)
at com.google.cloud.ServiceOptions.getService(ServiceOptions.java:498)
at com.google.firebase.cloud.FirestoreClient.<init>(FirestoreClient.java:51)
at com.google.firebase.cloud.FirestoreClient.<init>(FirestoreClient.java:29)
at com.google.firebase.cloud.FirestoreClient$FirestoreClientService.<init>(FirestoreClient.java:95)
at com.google.firebase.cloud.FirestoreClient.getInstance(FirestoreClient.java:85)
at com.google.firebase.cloud.FirestoreClient.getFirestore(FirestoreClient.java:78)
at com.google.firebase.cloud.FirestoreClient.getFirestore(FirestoreClient.java:64)
at utils.FirebaseClient.<clinit>(FirebaseClient.kt:10)
at utils.FirestoreCollectionsKt.<clinit>(FirestoreCollections.kt:5)
at content.ContentTasks.getExistingContentSet(ContentTasks.kt:89)
at content.ContentTasks.run(ContentTasks.kt:39)
at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
at java.base/java.util.TimerThread.run(Timer.java:506)

Process finished with exit code 0

现有身份验证代码

Initialization.kt

object Initialization {
    @JvmStatic
    fun main(args: Array<String>) {
        set(Enums.EnvironmentType.STAGING)
        initializeFirestore()
        getContent()
    }

    private fun initializeFirestore() {
        FirebaseApp.initializeApp(FirebaseOptions.Builder()
            .setCredentials(fromStream(Gson().toJson(getFirebaseCredentials()).byteInputStream()))
            .setFirestoreOptions(FirestoreOptions.newBuilder().setTimestampsInSnapshotsEnabled(true).build())
            .build())
    }
}

CredentialsHelper.kt

fun getFirebaseCredentials() =
    if (environmentType == PRODUCTION) FirebaseCredentials(
            "service_account",
            "[projectId]",
            "[privateKeyId]",
            "[privateKey]",
            "[clientEmail]",
            "[clientId]",
            "https://accounts.google.com/o/oauth2/auth",
            "https://accounts.google.com/o/oauth2/token",
            "https://www.googleapis.com/oauth2/v1/certs",
            "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-0z4rn%40carpecoin-media-211903.iam.gserviceaccount.com")
    else FirebaseCredentials(
            "service_account",
            "[projectId]",
            "[privateKeyId]",
            "[privateKey]",
            "[clientEmail]",
            "[clientId]",
            "https://accounts.google.com/o/oauth2/auth",
            "https://accounts.google.com/o/oauth2/token",
            "https://www.googleapis.com/oauth2/v1/certs",
            "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-dhr30%40coinverse-media-staging.iam.gserviceaccount.com")

尝试的解决方案

build.gradle 中更新库版本,以防问题是由于依赖冲突引起的。

build.gradle

buildscript {
    ext.kotlin_version = '1.3.41'
    ext.junitJupiterVersion  = '5.5.0'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
    }
}

plugins {
    id 'java'
    id 'org.jetbrains.kotlin.jvm' version '1.2.51'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    testImplementation group: 'junit', name: 'junit', version: '5.3.2'
    // JUnit Jupiter API and TestEngine implementation
    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
    testImplementation "org.assertj:assertj-core:3.12.2"
    // To avoid compiler warnings about @API annotations in JUnit code
    testCompileOnly 'org.apiguardian:apiguardian-api:1.1.0'
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.6.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'com.google.firebase:firebase-admin:6.8.1'
    implementation 'com.google.cloud:google-cloud-storage:1.79.0'
    implementation 'com.google.apis:google-api-services-youtube:v3-rev212-1.25.0'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

1 个答案:

答案 0 :(得分:0)

这是因为“ firebase-admin”通过“ gax-grpc”依赖的“ gax”较旧,因此其他库(在您的情况下可能是“ google-cloud-storage”)引入了较新的“ gax”由Gradle选择,导致兼容性问题。

就我而言,强迫Gradle使用最新的“ gax-grpc”(如下所示)可以解决问题。希望对您有帮助。

implementation group: 'com.google.api', name: 'gax-grpc', version: '1.47.1'