我知道你可以在web,ios和android中提取服务器时间戳 - 但是新的Cloud Functions for Firebase呢?我无法弄清楚如何获得服务器时间戳?用例是我想在收到电子邮件时为其添加时间戳.
在Web上,它是Firebase.database.ServerValue.TIMESTAMP
但这似乎在功能节点服务器接口中不可用?
我觉得已经很晚了,我可能会忽略这一点......
编辑
我正在这样初始化
admin.initializeApp(functions.config().firebase);
const fb = admin.database()
Run Code Online (Sandbox Code Playgroud)
然后,就像这样被调用..
Firebase.database.ServerValue.TIMESTAMP
Run Code Online (Sandbox Code Playgroud)
但是,这是来自客户端集成.在功能上,Firebase未像这样初始化.我试过了
admin.database().ServerValue.TIMESTAMP
Run Code Online (Sandbox Code Playgroud)
和
fb.ServerValue.TIMESTAMP
Run Code Online (Sandbox Code Playgroud) node.js firebase firebase-realtime-database google-cloud-functions firebase-admin
我有点困惑为什么这段代码在已经正常工作一周的情况下突然决定失败。
import firebase_admin
from firebase_admin import firestore, credentials,db, auth
def userIsLoggedIn(self,id_token: str) -> bool:
try:
decoded_token = auth.verify_id_token(id_token)
self.uid = decoded_token['uid']
except Exception as e:
return False,str(e)
return True,""
Run Code Online (Sandbox Code Playgroud)
返回的错误消息是:“HTTPResponse”对象没有“strict”属性并且当我在云服务器而不是本地主机上进行测试时,我只能复制此错误。
我查看了堆栈跟踪,发现这是auth.verify_id_token导致问题的函数,具体来说:
ile "/usr/local/lib/python3.11/site-packages/cachecontrol/serialize.py", line 54, in dumps
2023-05-04T16:11:04.767062340Z u"strict": response.strict,
2023-05-04T16:11:04.767067540Z ^^^^^^^^^^^^^^^
2023-05-04T16:11:04.767072540Z AttributeError: 'HTTPResponse' object has no attribute 'strict'
Run Code Online (Sandbox Code Playgroud)
编辑:
好吧,帕特里克下面给我指出了一个链接,告诉我:
“这似乎是缓存控制与新版本的 urllib3 2.0 不兼容。strict 不再是 HTTPResponse 类上受支持的参数。目前,您可能需要固定到旧版本的 urllib3 或使用缓存控制团队更新他们的使用情况。”
我得看看现在我能不能做点什么。例如,按照帕特里克的建议,使用 urllib3 2.0.0 作为解决方法。
我想使用Firebase(身份验证,数据库和存储)构建Node.js Web应用程序.但我对应该使用哪个模块,firebase或firebase-admin(或两者)感到困惑.
node.js npm firebase firebase-realtime-database firebase-admin
我想通过NodeJS与Firebase进行身份验证并保持会话.客户端无法直接与Firebase通信.
简而言之:
客户端(浏览器)<----> NodeJs(firebase-admin)<----> Firebase
我在NodeJS中创建了Firebase客户端,然后我使用了登录方法:
var firebaseClient = require('firebase');
firebaseClient.initializeApp(config)
firebaseClient.auth().signInWithEmailAndPassword(req.body.email, req.body.password).catch(function(error){
console.log(error);
})
Run Code Online (Sandbox Code Playgroud)
然后我创建了路由来检查经过身份验证的用户:
app.get('/check',function(req,res){
var user = firebaseClient.auth().currentUser
console.log(user)
})
Run Code Online (Sandbox Code Playgroud)
此方法仅允许我保留1个先前登录的用户.
我想使用firebase-admin,但我不知道如何保持会话和验证用户
我正在尝试按照文档连接到我的 Firebase 应用程序,但 ESLint 正在抱怨。我已经检查过相关问题,但那里提出的解决方案似乎对我不起作用。我有以下 .eslint.js 文件:
module.exports = {
extends: [
'airbnb-typescript/base',
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:typescript-sort-keys/recommended',
'plugin:import/recommended',
'prettier'
],
parser : '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
modules: true
},
ecmaVersion: 6,
project : './tsconfig.json',
sourceType : 'module'
},
plugins: ['@typescript-eslint', 'typescript-sort-keys', 'sort-keys-fix'],
rules : { ... }
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 firebase admin sdk 运行节点服务器。但是,每次我尝试从服务器发送推送通知时,都会收到 401 错误。
这是我收到的确切错误:
errorInfo: {
code: 'messaging/authentication-error',
message: 'An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. Raw server response: "<HTML>\n' +
'<HEAD>\n' +
'<TITLE>PROJECT_NOT_PERMITTED</TITLE>\n' +
'</HEAD>\n' +
'<BODY BGCOLOR="#FFFFFF" TEXT="#000000">\n' +
'<H1>PROJECT_NOT_PERMITTED</H1>\n' +
'<H2>Error 401</H2>\n' +
'</BODY>\n' +
'</HTML>\n' +
'". Status code: 401.'
},
codePrefix: 'messaging'
Run Code Online (Sandbox Code Playgroud)
我不太清楚为什么我没有该项目的权限。我已经设置了我的服务帐户,并下载了 .json 文件。我什至进入 gcloud 平台并尝试添加任何看起来正确的权限。以下是与我的服务帐户关联的所有权限:
我在本地运行服务器,并像这样初始化应用程序:
const …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码通过 FCM 从 Firebase Cloud 函数发送多播通知:
const message = {
tokens: recipients,
notification: {
title: title,
body: body
},
data: {
projectPartnerId: projectPartnerId
}
};
return admin.messaging().sendMulticast(message);
Run Code Online (Sandbox Code Playgroud)
并且没有发送任何推送通知。每个响应都包含一个带有相同消息的错误:“未找到请求的实体”。
我在 Google Cloud 控制台中启用了 API(Firebase 文档中没有提到它,但显然这是必要的)。我不知道我还能做什么。我能找到的所有其他问题都与 HTTP API 或遗留 API 相关。我使用的是最新版本的 Firebase Admin SDK。
我正在尝试创建一个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进行身份验证吗?
谢谢你的帮助。
javascript firebase google-cloud-functions firebase-admin google-auth-library
目前我正在nodejs上创建Firebase API.我想在nodejs上使用firebase-admin处理所有Firebase内容(例如身份验证).但是,如果没有客户端的Javascript Firebase SDK,在firebase-admin中通过nodejs对用户进行身份验证的正确方法是什么?在admin的官方文档中,我没有找到一个名为signInWithEmailAndPassword的函数(就像在客户端SDK上一样)用于nodejs.只有一个名为" getUserByEmail "的函数,但此函数不会检查用户是否输入了正确的密码.
这是我的表格:
<form class="sign-box" action="/login" method="post">
<div class="form-group">
<input id="username" name="username" type="text" class="form-control" placeholder="E-Mail"/>
</div>
<div class="form-group">
<input id="password" name="password" type="password" class="form-control" placeholder="Password"/>
</div>
<button type="submit" class="btn btn-rounded">Sign in</button>
</form>
Run Code Online (Sandbox Code Playgroud)
提交表单后,我将值传递给nodejs中的API:
app.post('/login', urlencodedParser, function (req, res) {
// getting the values
response = {
username: req.body.username,
password: req.body.password
};
// authenticate the user here, but how ?
});
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是在客户端使用Firebase SDK使用signInWithEmailAndPassword登录并获取uid.一旦我有了UID,我想将UID发送到nodejs并调用函数createCustomToken并将生成的令牌(带有一些额外的声明)返回给客户端.一旦我得到令牌,我将使用函数signWithCustomToken(在客户端)来验证用户.这种方式是正确的还是有更好的方法?
我最近在尝试部署到Firebase时开始收到以下错误(过去成功完成之后).我不确定这可能会有什么变化才能开始发生.如果我运行firebase服务在localhost上服务,一切正常.我的package.json和index.js的要求也在下面.
i deploying functions, hosting
i functions: ensuring necessary APIs are enabled...
i runtimeconfig: ensuring necessary APIs are enabled...
+ runtimeconfig: all necessary APIs are enabled
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (46.02 KB) for uploading
+ functions: functions folder uploaded successfully
i hosting: preparing public directory for upload...
! Warning: Public directory does not contain index.html
+ hosting: 9 files uploaded successfully
i starting release process (may …Run Code Online (Sandbox Code Playgroud) firebase-admin ×10
firebase ×9
node.js ×4
javascript ×2
eslint ×1
flask ×1
npm ×1
typescript ×1