将 firebase privateKey 作为 Heroku 配置变量的问题 [英] Escaping issue with firebase privateKey as a Heroku config variable
问题描述
我正在尝试创建一个 Heroku 节点任务,该任务从 Firebase 读取数据并通过 console.log 对其进行记录.
I'm trying to create an Heroku node task that reads data from Firebase and console.log it.
我的节点脚本(位于/bin 目录内)是:
My node script (located inside the /bin directory) is:
require('dotenv').config({ silent: true });
var firebase = require('firebase');
firebase.initializeApp({
serviceAccount: {
projectId: process.env.fb_projectId,
clientEmail: process.env.fb_clientEmail,
privateKey: process.env.fb_privateKey
},
databaseURL: process.env.fb_databaseURL
})
console.log(process.env.fb_privateKey);
firebase.database().ref('tasks').once('value', function(snapshot) {
console.log(snapshot.val());
process.exit();
})
所有环境变量都是正确的,除了 fb_privateKey
,因为它包含
个字符.
All env variables are correct, except fb_privateKey
, since it contains
characters.
我的本地 .env
文件和 Heroku 环境变量都包含一个名为 fb_privateKey
的键和一个带有
字符的字符串形式的值 -用 "
包围.
Both my local .env
file and Heroku environment variables contains a key named fb_privateKey
and a value as a string with
characters - surrounded with "
.
fb_privateKey="-----BEGIN PRIVATE KEY-----
MY-PRIVATE-KEY
-----END PRIVATE KEY-----
"
当我在本地运行脚本时,它会用新行+从 Firebase 返回的任务记录我的私钥.但是,当我在 Heroku 上运行它时, (heroku run myScript
) 将我的私钥记录为
字符(作为单行)并且无法从中读取数据Firebase(可能是由于私钥错误).有什么想法吗?
When I run the script locally, it logs me the private key with new lines + the tasks that were returned from Firebase. However, when I run it on Heroku, (heroku run myScript
) is logs me private key with
characters (as a single line) and fails to read the data from Firebase (probably due to a bad private key). Any ideas?
推荐答案
我今天遇到了同样的问题.您需要通过将 \n
字符替换为
来清理读取的私钥.
I had the same problem today. You need to sanitize the read private key by replacing \n
characters with
.
admin.initializeApp({
credential: admin.credential.cert({
"projectId": process.env.FIREBASE_PROJECT_ID,
"private_key": process.env.FIREBASE_PRIVATE_KEY.replace(/\n/g, '
'),
"clientEmail": process.env.FIREBASE_CLIENT_EMAIL,
}),
databaseURL: process.env.FIREBASE_DATABASE_URL,
});
这篇关于将 firebase privateKey 作为 Heroku 配置变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!