如何在节点 js 中加载 firebase 管理密钥?
How to load a firebase admin key in node js?
我想使用 firebase admin SDK 让我的节点服务器可以免费访问我的数据库。这是我的 functions
文件夹中 index.js
的代码:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert("logininfo.json"),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});
在同一个文件夹中,我有我的 logininfo.json,它看起来像这样(出于显而易见的原因对密钥进行了审查):
{
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "..."
}
但是,我在尝试部署到 firebase 托管时收到错误 Failed to parse certificate key file: Error: ENOENT: no such file or directory
。
我该如何解决这个问题,是否有更安全/更优雅的方式来处理这个问题?我可以只更改 firebase 托管中某处的 GOOGLE_APPLICATION_CREDENTIALS 变量吗?
可以通过两种不同的方式完成:
按路径:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const serviceAccount = require("path/to/logininfo.json");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});
按对象:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert({
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":
"https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "..."
}),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});
我想使用 firebase admin SDK 让我的节点服务器可以免费访问我的数据库。这是我的 functions
文件夹中 index.js
的代码:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert("logininfo.json"),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});
在同一个文件夹中,我有我的 logininfo.json,它看起来像这样(出于显而易见的原因对密钥进行了审查):
{
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "..."
}
但是,我在尝试部署到 firebase 托管时收到错误 Failed to parse certificate key file: Error: ENOENT: no such file or directory
。
我该如何解决这个问题,是否有更安全/更优雅的方式来处理这个问题?我可以只更改 firebase 托管中某处的 GOOGLE_APPLICATION_CREDENTIALS 变量吗?
可以通过两种不同的方式完成:
按路径:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const serviceAccount = require("path/to/logininfo.json");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});
按对象:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
// Initialize app
admin.initializeApp({
credential: admin.credential.cert({
"type": "service_account",
"project_id": "...",
"private_key_id": "...",
"private_key": "...",
"client_email": "...",
"client_id": "...",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":
"https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "..."
}),
databaseURL: "https://thenameofmydatabase.firebaseio.com/",
databaseAuthVariableOverride: {
uid: "nameserver"
}
});