[英]Firebaseapperror: failed to parse private key: error: invalid pem formatted message
I got this error firebaseapperror: failed to parse private key: error: invalid pem formatted message
when I pushed my nodejs app to heroku.当我将我的 nodejs 应用程序推送到 heroku 时,我得到了这个错误firebaseapperror: failed to parse private key: error: invalid pem formatted message
。
I had my environment variables all set like this我的环境变量都是这样设置的
# .env file
project_id=project_id
private_key='-----BEGIN PRIVATE KEY----- ... ------END PRIVATE KEY-----\n'
client_email=client_email
And accessed them like so:并像这样访问它们:
export const sa = {
privateKey: process.env.private_key,
projectId: process.env.project_id,
clientEmail: process.env.client_email
};
On my local everything worked fine, but on production (heroku) I got the error mentioned above.在我的本地一切正常,但在生产(heroku)上我得到了上面提到的错误。 The private key is a multiline environment variable and it couldn't be parsed.私钥是多行环境变量,无法解析。
Please, how do I fix this?请问,我该如何解决这个问题?
The Fix修复
How I fixed this was through a post I saw online: How to Store a Long, Multi-line Private Key in an Environment Variable我如何解决这个问题是通过我在网上看到的一篇文章: 如何在环境变量中存储长的多行私钥
Follow the step and you should fix this as well.按照步骤操作,您也应该解决此问题。
Brief summary of the post is this:这篇文章的简要总结是这样的:
Store the long, multi-line key as a json string like so像这样将长的多行密钥存储为 json 字符串
Then parse it and destruction the key like so然后解析它并像这样销毁密钥
This would work on local, but on production (heroku) you will get a parse error because of the single quote before and after the very key in question.这将在本地工作,但在生产(heroku)上,由于有问题的关键之前和之后的单引号,你会得到一个解析错误。 Therefore, remove the single quotes before and after the key in your production env variable .因此,删除生产环境变量中键前后的单引号。 I also tried without the single quote on local and it worked .我也试过在 local 上不加单引号,它起作用了。
Extra Benefit额外福利
Extra Benefit with this Method这种方法的额外好处
From this, you could make life easy for you by doing this由此,您可以通过这样做让生活变得轻松
Store the entire object as json string in your env variable.将整个 object 作为 json 字符串存储在您的环境变量中。
Then access it like so然后像这样访问它
That's it.而已。
Reminder: remember what I said about single quote提醒:记住我所说的单引号
Alternatively, getting rid of single/double quotes around private_key
solved it in my case.或者,去掉private_key
周围的单引号/双引号解决了我的问题。 You can set private key in your prod env variables as:您可以在 prod env 变量中将私钥设置为:
-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----
The most consistent result for me was to transform the json to base64 string and then JSON.parse(Buffer.from(process.env.CERT, 'base64').toString())
对我来说最一致的结果是将 json 转换为 base64 字符串,然后转换为JSON.parse(Buffer.from(process.env.CERT, 'base64').toString())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.