使用环境变量对 Firebase Admin SDK 进行身份验证

Authenticating with the Firebase Admin SDK using environment variable

问题

当我尝试使用环境变量对 Cloud Firestore 进行身份验证时,我收到了 ENAMETOOLONG 错误。我在很多地方搜索了文档。如果有人能给我指出正确的方向,那将很有用。

正在创建环境变量

我创建了一个环境变量来加载我的服务帐户密钥

export MY_CREDENTIALS=$(cat myGoogleServiceAccountKey.json)

我的代码

const admin = require('firebase-admin');

var serviceAccount = process.env.MY_CREDENTIALS;
// console.log(`Service account = ${serviceAccount}`);

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});

const db = admin.firestore();

错误

当我 运行 从我的环境变量中获取我的服务帐户密钥的节点脚本时,我收到以下错误:

/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/auth/credential.js:142
            throw new error_1.FirebaseAppError(error_1.AppErrorCodes.INVALID_CREDENTIAL, 'Failed to parse certificate key file: ' + error);
            ^

Error: Failed to parse certificate key file: Error: ENAMETOOLONG: name too long, open '{
  "type": "service_account",
  "project_id": "myProject",
  "private_key_id": "123456789012345678901234567890",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMy private key\n-----END PRIVATE KEY-----\n",
  "client_email": "firebase-adminsdk-6ju1c@myProject.iam.gserviceaccount.com",
  "client_id": "12345678901234567890",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-myProject.iam.gserviceaccount.com"
}'
    at FirebaseAppError.Error (native)
    at FirebaseAppError.FirebaseError [as constructor] (/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseAppError (/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/utils/error.js:119:28)
    at Function.Certificate.fromPath (/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/auth/credential.js:142:19)
    at new CertCredential (/home/jason/Downloads/projects/myProjecte/functions/node_modules/firebase-admin/lib/auth/credential.js:192:45)
    at Object.cert (/home/jason/Downloads/projects/myProject/functions/node_modules/firebase-admin/lib/firebase-namespace.js:237:58)
    at Object.<anonymous> (/home/jason/Downloads/projects/myProject/myScript.js:7:34)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)

因此,在查看您收到的错误时,我注意到它显示 Error: ENAMETOOLONG: name too long, open '{。当向 admin.credential.cert 函数传递一个字符串时,它假定您正在尝试打开一个具有该字符串名称的文件。如果将其转换为一个对象,那么它应该能够读取凭据。

const admin = require('firebase-admin');

var serviceAccount = process.env.MY_CREDENTIALS;
// console.log(`Service account = ${serviceAccount}`);

admin.initializeApp({
    credential: admin.credential.cert(JSON.parse(serviceAccount))
});

const db = admin.firestore();