我正在学习Firebase和Node.js.我想从Twitter获取我的推文并将其发送到Firebase。从Twitter获取推文我没有问题。我的问题是如何将数据发送到Firebase?
我尝试了以下代码:
var firebase = require('firebase');
// Initialize
var app = firebase.initializeApp({
ServiceAccount: {
projectId: "******",
clientEmail: "****@gmail.com",
privateKey: "-----BEGIN PRIVATE KEY-----\nkey\n-----END PRIVATE KEY-----\n"
},
databaseURL: "****.firebaseio.com"
});
// Set Sample Data
firebase.database().ref('/').set({
username: "test",
email: "test@mail.com"
});
我在控制台上遇到以下错误:
Debugger listening on port 5858
crypto.js:279
var ret = this._handle.sign(toBuf(key), null, passphrase);
^
Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
at Error (native)
at Sign.sign (crypto.js:279:26)
at Object.sign (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\node_modules\jws\node_modules\jwa\index.js:54:45)
at Object.jwsSign [as sign] (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\node_modules\jws\lib\sign-stream.js:23:24)
at Object.JWT.sign (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\index.js:137:16)
at authJwt (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:83:16)
at fetchAccessToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:96:17)
at app_.INTERNAL.getToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:196:14)
at Zb.getToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\database-node.js:28:3496)
at yh (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\database-node.js:195:334)
Press any key to continue...
此外,这是我的firebase规则: {&#34;规则&#34;:{&#34; .read&#34;:true,&#34; .write&#34;:true}} < /强>
答案 0 :(得分:21)
我解决了我的问题。我刚从Firebase Console下载了serviceAccount json文件并将其插入到我的项目中。 “服务帐户”文件包含您需要的所有内容。
var firebase = require('firebase');
firebase.initializeApp({
databaseURL: 'https://*****.firebaseio.com',
serviceAccount: 'myapp-13ad200fc320.json', //this is file that I downloaded from Firebase Console
});
然后下面的代码很好用。
firebase.database().ref('/').set({
username: "test",
email: "test@mail.com"
});
答案 1 :(得分:14)
不推荐使用def readMany = {
val db = Database.forConfig("db1")
try {
val query = TableQuery[AnalysisDB]
val action = query.sortBy(_.name).result
db.run(action).map(_.map {
a => Analysis(a.sk, a.name)
})
} finally db.close
}
个应用中服务帐户的特定语法/库。在服务器(即,不是消费者应用程序,如物联网或桌面)上到达firebase的新方法是firebase admin sdk
。
您的初始化代码现在应该:
node
您仍然可以手动输入您的凭据,但现在他们已分配给不同的属性:
var admin = require("firebase-admin");
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});
答案 2 :(得分:9)
我们遇到了与AWS Beanstalk相同的问题。根本原因是因为\n
被私钥变量中的\\n
替换。
添加替换正则表达式后,它已修复:
"private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\\n/g, '\n')
将变量打印到日志中也有助于隔离问题。