我正在尝试创建一个Firebase云功能。因此,我将在本地运行我的Firebase云功能。 但是如何设置身份验证无效。
我已经安装了Firebase工具:https://firebase.google.com/docs/functions/local-emulator
我已经运行了命令firebase login
,所以现在登录了。
然后,通过本教程创建了json密钥:https://cloud.google.com/docs/authentication/getting-started
现在,如果我输入echo $GOOGLE_APPLICATION_CREDENTIALS
,则结果为/home/$USER/.google/****.json
,其中包含
"project_id","private_key_id","private_key","client_email", "client_id", "auth_uri", "token_uri", "auth_provider_x509_cert_url", "client_x509_cert_url"
我也尝试安装完整的google cloud sdk,然后运行:gcloud auth application-default login
,但没有成功。
Npm软件包版本:
"firebase-functions":"3.0.2"
"firebase-admin": "8.2.0"
我想我已经提供了足够的信息,但是如果您愿意,可以随时询问我更多信息。
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
const app = express();
app.get("/", async (req, res) => {
admin.firestore().collection('something').get().then((collection) =>
return res.send({"count": collection.docs.length, "status": 200});
});
exports.exports = functions.https.onRequest(app);
代码并不重要,最重要的是,即使我完成了所有这些步骤,当我使用firebase serve
在本地模拟Firebase并触发函数时,也会出现此错误:
错误:传入的JSON对象不包含client_email字段
我可以确保您的json文件包含client_email字段。
您能帮我与Google进行身份验证吗?
感谢您的帮助。
答案 0 :(得分:17)
我有类似的问题。 7.0.2
的{{1}}版本中可能存在错误。我回滚到firebase-tools
版,现在可以使用了。
因此临时解决方案是:
7.0.0
答案 1 :(得分:9)
简而言之:
admin.initializeApp({ credential: admin.credential.applicationDefault() });
请参阅admin.credential.applicationDefault()的文档
更新:请注意,仅建议将其用于测试/实验:
此策略在测试和实验时很有用,但可以使 很难确定您的应用程序正在使用哪个凭据。我们 建议明确指定应用程序使用的凭据 应该使用... Source
更多信息
在尝试本地调用firebase函数时,我遇到了相同的问题,该函数试图批量更新firestore数据库中的某些文档。 (没有分批测试)。
要开始在本地调用firebase函数,请使用:
firebase function:shell
您可能知道,这列出了项目的可用功能。
我调用了函数,并收到以下错误调用堆栈:
Unhandled error Error: The incoming JSON object does not contain a client_email field
> at JWT.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\jwtclient.js:165:19)
> at GoogleAuth.fromJSON (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:294:16)
> at GoogleAuth.getClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-auth-library\build\src\auth\googleauth.js:476:52)
> at GrpcClient._getCredentials (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:107:40)
> at GrpcClient.createStub (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\google-gax\build\src\grpc.js:223:34)
> at new FirestoreClient (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\v1\firestore_client.js:128:39)
> at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:315:26)
> at ClientPool.acquire (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:61:35)
> at ClientPool.run (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\pool.js:114:29)
> at Firestore.readStream (D:\thdk\Projects\timesheets\functions\node_modules\firebase-admin\node_modules\@google-cloud\firestore\build\src\index.js:995:26)
RESPONSE RECEIVED FROM FUNCTION: 500, {
"error": {
"status": "INTERNAL",
"message": "INTERNAL"
}
}
我正在使用命令行在本地运行我的函数:
firebase functions:shell
我正在使用以下代码:
// Reference report in Firestore
const db = admin.firestore();
admin.initializeApp();
export const performMyCallableFirebaseFunction = (db, { from, to }) => {
return db.collection("collectionName").where("prop", "==", from).limit(500).get().then(snapshot => {
if (snapshot.empty) return new Promise(resolve => resolve(`No docs found with prop: ${from}`));
const batch = db.batch();
snapshot.forEach(doc => batch.update(doc.ref, { prop: to }));
return batch.commit();
});
};
exports.myCallableFirebaseFunction = functions.https.onCall(data => performMyCallableFirebaseFunction(db, data.from, data.to));
我换了线
admin.initializeApp();
到
admin.initializeApp({ credential: admin.credential.applicationDefault() });
现在我可以使用以下命令在本地调用函数:
firebase functions:shell
firebase > myCallableFirebaseFunction({from: "foo", to: "bar"})
答案 2 :(得分:3)
您可能需要设置Firebase Admin SDK才能使用Firebase模拟器。您可以通过在调用credential
方法时传递一个admin.initializeApp()
属性来实现:
const serviceAccount = require('../serviceAccount.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
您可以在Firebase控制台中下载服务帐户JSON文件:
答案 3 :(得分:2)
创建Firebase项目后,您可以使用将您的服务帐户文件和Google Application Default Credentials结合在一起的授权策略来初始化SDK。
要对服务帐户进行身份验证并授权其访问Firebase服务,您必须生成JSON格式的私钥文件。
为您的服务帐户生成私钥文件:
在Firebase控制台中,打开“设置”>“服务帐户”。
单击“生成新私钥”,然后单击“生成密钥”进行确认。
安全地存储包含密钥的JSON文件。
将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为包含您的服务帐户密钥的JSON文件的文件路径。此变量仅适用于当前的Shell会话,因此,如果您打开一个新的会话,请再次设置该变量。
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
答案 4 :(得分:2)
这是我经过几个小时的努力后才解决的问题:
简短回答:
创建Firebase-adminsdk密钥
操作方法:
转到Google-cloud-platform > Service accounts
https://console.cloud.google.com/iam-admin/serviceaccounts/
选择您的项目
firebase-admin-sdk
的{{1}} firebase-adminsdk-u4k3i@example..
并选择Create key
JSON
。其中有.json
ProjectID, PrivateKey and ClientEmail
答案 5 :(得分:0)
运行firebase emulators:start
时出现此错误。
根据对此错误https://github.com/firebase/firebase-tools/issues/1451的调查,看来这是直接引用应用程序而不是通过管理模块来解决的问题。
即这会导致错误:
const app = admin.initializeApp();
const firestore = app.firestore();
但这不是:
admin.initializeApp();
const firestore = admin.firestore();
但是对于原始问题,您正在使用admin.firestore(),所以这不是问题。似乎从未调用过admin.initializeApp()
。也许这可能是您遇到问题的原因?