我一直在尝试将eclipse ee上的动态Web项目连接到firebase。我已经上传了所有必需的依赖项(来自maven项目)。
以下是我直接从Java上的firebase文档中使用的代码。
public class FirebaseUpdater {
public static void main(String[] args){
FileInputStream serviceAccount;
try {
serviceAccount = new
FileInputStream("C:/Users/Asus/workspace/school-dy2"
+ "/schoolbackup_service.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://schoolbackup-f2429.firebaseio.com/")
.build();
FirebaseApp.initializeApp(options);
// As an admin, the app has access to read and write all data,
//regardless of Security Rules
DatabaseReference ref = FirebaseDatabase.getInstance()
.getReference();
DatabaseReference attendance = ref.child("Attendance1");
attendance.setValueAsync("Debarred");
System.out.println(attendance.toString());
attendance.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Datasnapshot Received");
Object document = dataSnapshot.getValue();
System.out.println(dataSnapshot.getKey());
}
public void onCancelled(DatabaseError error) {
}
});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我还使用了正确的程序来获取服务帐户密钥(Firebase控制台 - >用户和权限 - >服务帐户 - >生成新的私钥)
数据库安全规则也是公开的(因为我有服务帐户密钥,所以它不应该是一个问题):
{
"rules": {
".read": true,
".write": true
}
}
项目编译成功,互联网连接不成问题。
我也检查过工作区网址。它正确地将我带到浏览器上的数据库。
然而,尽管如此,我的项目似乎并没有连接到firebase和更新值。
有人能告诉我可能出现的问题吗?我在这里走到了尽头。
请注意我正在使用eclipse ee(动态网络项目),我直接导入了com.google.firebase(groupID),firebase-admin(artifactID),6.2.0(版本)的maven依赖项。
难道我不能将maven依赖项用于动态Web项目吗?服务帐户密钥方法是问题吗?有没有可行的解决方法呢?
编辑: 我尝试打印GoogleCredentials.fromStream(serviceAccount)以查看错误。这就是我得到的:
ServiceAccountCredentials{clientId=109785559732337174527, clientEmail=firebase-adminsdk-0into@schoolbackup-f2429.iam.gserviceaccount.com, privateKeyId=5512743509a43e1371cf88ec3feba4007abe4bf1, transportFactoryClassName=com.google.auth.oauth2.OAuth2Utils$DefaultHttpTransportFactory, tokenServerUri=https://accounts.google.com/o/oauth2/token, scopes=[], serviceAccountUser=null}
serviceAccountUser为null。这可能是个问题吗?如果是,我该如何解决呢。