Shi*_*ing 21 javascript firebase google-cloud-functions firebase-admin google-auth-library
我正在尝试创建一个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"
Run Code Online (Sandbox Code Playgroud)
我也尝试安装完整的google cloud sdk并运行:gcloud auth application-default login但是没有成功。
Npm软件包版本:
"firebase-functions":"3.0.2"
"firebase-admin": "8.2.0"
Run Code Online (Sandbox Code Playgroud)
我想我已经提供了足够的信息,但是如果您愿意,可以随时问我更多。
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);
Run Code Online (Sandbox Code Playgroud)
代码并不重要,最重要的是,即使我已经完成了所有这些步骤,当我在本地模拟Firebase firebase serve并触发函数时,却出现此错误:错误:传入的JSON对象不包含client_email领域
我可以确保您的json文件包含client_email字段。
您可以帮我与Google进行身份验证吗?
谢谢你的帮助。
tom*_*exx 18
我有一个类似的问题。可能是的版本7.0.2中的错误firebase-tools。我回滚到版本7.0.0,现在可以使用了。
因此,临时解决方案是:
npm i firebase-tools@7.0.0 -g
Run Code Online (Sandbox Code Playgroud)
Thd*_*hdK 10
简而言之:
admin.initializeApp({ credential: admin.credential.applicationDefault() });
Run Code Online (Sandbox Code Playgroud)
请参阅docs.admin.credential.applicationDefault()
更新:请注意,仅建议将其用于测试/实验:
在测试和试验时,此策略很有用,但可能使您难以分辨您的应用程序正在使用哪个凭据。我们建议您明确指定应用程序应使用的凭据,... 源
更多信息
在尝试本地调用firebase函数时,我遇到了同样的问题,该函数试图批量更新firestore数据库中的某些文档。(没有批量测试)。
要开始在本地调用firebase函数,我使用:
firebase function:shell
Run Code Online (Sandbox Code Playgroud)
您可能知道,它列出了项目的可用功能。
我调用了函数,并收到以下错误调用堆栈:
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"
}
}
Run Code Online (Sandbox Code Playgroud)
我使用命令行在本地运行函数:
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));
Run Code Online (Sandbox Code Playgroud)
我换了线
admin.initializeApp();
至
admin.initializeApp({ credential: admin.credential.applicationDefault() });
现在我可以使用以下命令在本地调用函数:
firebase functions:shell
firebase > myCallableFirebaseFunction({from: "foo", to: "bar"})
Run Code Online (Sandbox Code Playgroud)
请参阅docs.admin.credential.applicationDefault()
您可能需要设置 Firebase Admin SDK 才能使用 Firebase 模拟器。credential您可以通过在调用方法时传递属性来实现admin.initializeApp():
const serviceAccount = require('../serviceAccount.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
Run Code Online (Sandbox Code Playgroud)
您可以在 Firebase 控制台中下载服务帐户 JSON 文件: