我正在尝试在我的函数文件夹中初始化 firebase admin sdk,以便我的云函数可以通过管理员读/写访问权限访问我的数据库,但是当我尝试部署我的函数时,会返回以下错误
错误:解析触发器时出错:找不到模块“/serviceKey.json”
我尝试在函数目录中运行 npm install 但没有帮助。
这是我的 package.json 文件
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"cors": "^2.8.4",
"express": "^4.16.3",
"firebase": "^5.3.1",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2"
},
"private": true
}
Run Code Online (Sandbox Code Playgroud)
这是我的 index.js 文件
{
const functions = require('firebase-functions');
const express = require('express');
const cors = require('cors')({origin: true});
var firebase = require("firebase"); …Run Code Online (Sandbox Code Playgroud) node.js firebase firebase-realtime-database google-cloud-functions firebase-admin
我正在尝试通过私钥和管理员权限使用管理员权限访问 Firestore,以便我可以将其用于后端的查询和内容。
//i import fb-admin
const firebaseAdmin = require('firebase-admin')
//i initialize as admin with a valid credential
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert('./pathto/myprivatekeyinfo.json')
})
//i initialize firestore
var dba = firebaseAdmin.firestore()
//i test the connection by listing all the available collections
dba.getCollections().then(collectionList => {
collectionList.forEach(collection => {
console.log(collection.id) //print available collections
}
},error => console.error(JSON.stringify(error,null," "))) //print error
Run Code Online (Sandbox Code Playgroud)
它看起来与文档中的说明完全一样,但我的问题是,对于我尝试在其中执行 Node 的某些计算机,我只能在少数计算机上随机连接。
有时我连接并且一切顺利,按应有的方式打印可用的集合,但大多数时候我得到的只是无效凭据的错误
{
"code": 16,
"metadata": {
"_internal_repr": {
"www-authenticate": [
"Bearer realm=\"https://accounts.google.com/\""
]
}
},
"details": "Request had invalid authentication credentials. Expected …Run Code Online (Sandbox Code Playgroud) javascript node.js firebase firebase-admin google-cloud-firestore
我有一个文档集合,所有文档都有随机 id 和一个名为 的字段date。
docs = collection_ref.order_by(u'date', direction=firestore.Query.ASCENDING).get()
Run Code Online (Sandbox Code Playgroud)
想象一下我将搜索限制为前十个
docs = collection_ref.order_by(date', direction=firestore.Query.ASCENDING).limit(10).get()
Run Code Online (Sandbox Code Playgroud)
当我想要获取 11 到 20 之间的项目时,如何继续下一个查询?
我无法使用 Python 在 Google Firebase (Cloud Firestore) 数据库中获取、更新或创建文档。
我拥有的:
B) 凭证 JSON 文件另存为test.json(path/to/serviceKey.json在文档中经常调用),如下所示(已编辑):
{
"type": "service_account",
"project_id": "test-6f02d",
"private_key_id": "fffca ... 5b7",
"private_key": "-----BEGIN PRIVATE KEY-----\n ... 1IHE=\n-----END PRIVATE KEY-----\n",
"client_email": "test-admin@test-6f02d.iam.gserviceaccount.com",
"client_id": "112 ... 060",
"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": "https://www.googleapis.com/robot/ ... .gserviceaccount.com"
}
Run Code Online (Sandbox Code Playgroud)
此用户具有角色Owner。
C)firebase_admin 安装(使用virtualenv,pip),我可以这样做:
{
"type": "service_account",
"project_id": "test-6f02d",
"private_key_id": "fffca ... 5b7",
"private_key": "-----BEGIN PRIVATE KEY-----\n ... 1IHE=\n-----END PRIVATE KEY-----\n",
"client_email": "test-admin@test-6f02d.iam.gserviceaccount.com",
"client_id": "112 ... …Run Code Online (Sandbox Code Playgroud) 我正在使用 python 和 firestore,并尝试在后端创建一个客户端。我正在关注这个教程
用下面的代码
import firebase_admin
from firebase_admin import credentials, firestore
cred = credentials.Certificate("cred_file.json")
firebase_admin.initialize_app(cred)
db = firestore.Client()
ref = db.collections(u'table')
Run Code Online (Sandbox Code Playgroud)
并收到以下错误
google.auth.exceptions.DefaultCredentialsError:无法自动确定凭据。请设置 GOOGLE_APPLICATION_CREDENTIALS 或显式创建凭据并重新运行应用程序。了解更多信息
我猜想默认凭据有问题,我不明白的是,如果我在代码中初始化凭据,为什么应用程序会不断抛出默认凭据的错误?我明确地给它正确的信用文件。
我正在尝试在我的 firebase 函数上使用 admin SDK 和 firestore api 检索文档列表。我尝试使用以下内容:
import * as admin from 'firebase-admin';
const adminFirestore = admin.firestore();
adminFirestore.collection('accounts');
collection.get().then(snapshot => {
snapshot.forEach(doc => {
console.log( doc.data() );
});
});
Run Code Online (Sandbox Code Playgroud)
但是,这不会打印该集合中的各个文档。我还尝试过以下方法:
const ref = adminDb.ref('accounts');
ref.once("value")
.then(function(snapshot) {
console.log(snapshot);
});
ref.once('value', (snapshot) => {
console.log(snapshot.val());
}).then(something => {
console.log(something);
})
Run Code Online (Sandbox Code Playgroud)
没有什么真正打印集合中的文档。我得到的只是对该集合的引用,并且不太清楚如何接收该集合中的条目。有什么建议吗?
node.js firebase google-cloud-functions firebase-admin google-cloud-firestore
我想通过使用 admin SDK 来使用 firebase 存储,因为使用 gcd 需要付费帐户。
我尝试了很多在没有付费帐户的情况下使用 gcd,但除了使用 gcd 存储很复杂之外,我没有找到一种方法来做到这一点。
我试图在 firebase 文档中找到答案,但提到的函数文档根本不起作用。所以我决定发布这个问题的答案,以便它可以帮助其他人
想为我的 Firebase 项目创建一个系统管理页面,有什么办法可以打破规则并在客户端浏览器上使用 firebase-admin SDK?
我遇到的第一个问题是在使用 admin initializeApp 函数成功初始化后使用 firebase/database SDK,我收到错误“TypeError: rtdb.initStandalone is not a function”。我应该继续尝试解决这个问题,还是换个角度来解决。理想情况下,我不想使用对服务器的调用,或者使用具有“超级用户”权限的 firebase auth。
我正在尝试防止存储滥用。我的目标是每天限制用户上传操作。所以我部署了一个存储 onFinalize 触发器,一旦文件上传,该功能就会检测到一个计数器,该计数器使用 Firestore 是否达到了限制。如果是,像这样将毫秒存储到 CustomUserClaims:
//the user need to wait after this time to continue upload files (timeToUnlock = currentTime + additionalTime)
setCustomUserClaims(uid, {
timeToUnlock: 1570509112055
})
Run Code Online (Sandbox Code Playgroud)
并比较规则
allow create: if request.time.toMillis() >= request.auth.token.timeToUnlock;
Run Code Online (Sandbox Code Playgroud)
但是每次当 setCustomUserClaims 我需要注销并再次登录客户端以更新声明或声明不会更新它只是保留以前的值。我打印 customClaims 到控制台参数已更新但似乎规则中的参数未更新。任何人都有更好的主意解决这个问题?因为如果这不起作用,我不知道要防止这种情况发生,谢谢您的帮助。
console.log((await admin.auth().getUser(uid)).customClaims);
Run Code Online (Sandbox Code Playgroud) google-cloud-storage firebase firebase-security firebase-authentication firebase-admin
我有一个 Firebase 云函数,我想创建一个 Callable 函数,以便我可以从内置身份验证的 Web 客户端调用它。但我也想使用 Admin SDK 从我自己的后端代码调用它(特别是来自Java)如果这很重要。
我看到该协议已记录在案,但我有点不清楚如何从 Admin SDK 对其进行身份验证。我知道您可以创建一个 token,但这似乎不是BearerCallable 函数所期望的正确标记。我怎样才能生成其中之一?或者是否有更好的方法从 Admin SDK 安全地调用 Callable 函数?
firebase firebase-authentication google-cloud-functions firebase-admin