【问题标题】:java: cannot access com.google.auth.Credentials class file for com.google.auth.Credentials not foundjava: 无法访问 com.google.auth.Credentials 类文件,因为找不到 com.google.auth.Credentials
【发布时间】:2019-03-11 07:19:53
【问题描述】:

我正在使用 firebase Admin SDK,但我在运行时收到此错误:

Error:(22, 36) java: cannot access com.google.auth.Credentials
class file for com.google.auth.Credentials not found

这是抛出错误的构造函数

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

class Database{

private FirebaseDatabase firebaseDatabase;

Database(){

    InputStream serviceAccount = Database.class.getResourceAsStream("reading-incentive-firebase-adminsdk-n556s-1b742e4b58.json");

    FirebaseOptions options;
    try {
        options = new FirebaseOptions.Builder()
                .setCredentials( GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://reading-incentive.firebaseio.com")
                .build();
        FirebaseApp.initializeApp(options);
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error loading database");
    }

    firebaseDatabase = FirebaseDatabase.getInstance();

}

}

这是第 22 行

.setCredentials(GoogleCredentials.fromStream(serviceAccount))

我在 IntelliJ 中使用 java sdk 1.8 和 gradle。我读过其他帖子并读到使用 sdk 1.8 over 1.7 会引发错误,但我找不到解决方案。这是我的 build.gradle 文件...

group 'src'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    implementation 'com.google.firebase:firebase-admin:6.5.0'
}

感谢您的帮助。

【问题讨论】:

  • 嗨 Andrew,你运行这个的环境是什么?是谷歌应用引擎吗?
  • 我没有使用谷歌应用引擎,我正在使用 JavaFX 在 Windows 上构建桌面应用。
  • 我发布了一个答案。让我知道它是否有效。

标签: java firebase gradle firebase-admin


【解决方案1】:

不久前,当我尝试在我们的应用引擎项目中进行 firebase 集成时,我遇到了同样的问题。这是由您在谷歌库中的应用程序中存在的某种暂时依赖引起的。

请尝试这些东西,

如果您使用的是较低版本的 appengine-api,请将其升级到 appengine-api-1.0-sdk-1.9.64.jar 或更高版本。

然后检查您的依赖项。如果您使用其中任何一种,

google-oauth-client-1.22.0.jar
google-oauth-client-appengine-1.22.0.jar
google-oauth-client-servlet-1.22.0.jar

这可能是冲突。尝试删除它们(您可能不需要它们)。希望能帮助到你。

【讨论】:

  • 抱歉,我的应用程序中没有任何这些依赖项。我注意到“google-oauth-client-1.25.0”在外部库文件夹中,但在我的本地源或我的 gradle 文件中没有。
  • so remove it man,只要你可以通过你的项目引用它,它可能会导致冲突。
  • 太棒了。很高兴我能帮忙..!
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 2018-02-04
相关资源
最近更新 更多