问题
I'm using the firebase Admin SDK and im getting this error at runtime:
Error:(22, 36) java: cannot access com.google.auth.Credentials
class file for com.google.auth.Credentials not found
This is the constructor that is throwing the error
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();
}
}
this is line 22
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
I'm using the java sdk 1.8 in IntelliJ along with gradle. I've read other posts and read that using sdk 1.8 over 1.7 throws the error but I can't find a solution. Here is my build.gradle file...
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'
}
Thanks for any help.
回答1:
I had the same issue a while back when I was trying to do the firebase integration in our app engine project. This is caused by a some sort of transient dependency you are having in your application within google libraries.
Please try these things,
if you are using lower version of appengine-api upgrade it to appengine-api-1.0-sdk-1.9.64.jar
or higher.
then check your dependencies. if you are using either one of these,
google-oauth-client-1.22.0.jar
google-oauth-client-appengine-1.22.0.jar
google-oauth-client-servlet-1.22.0.jar
that could be the conflict. try removing them (you might not need them). hope it helps.
来源:https://stackoverflow.com/questions/52671448/java-cannot-access-com-google-auth-credentials-class-file-for-com-google-auth-c