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

时间:2018-02-16 21:05:38

标签: node.js firebase google-cloud-firestore google-authentication firebase-admin

问题

当我尝试使用环境变量对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();

错误

当我运行从我的环境变量中获取serviceaccount键的节点脚本时,出现以下错误:

/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)

1 个答案:

答案 0 :(得分:6)

因此,当您查看错误时,我会注意到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();