我尝试在Firebase中编写一个云函数,默认情况下需要像普通云函数一样进行身份验证:
对于经典的 Cloud Functions,这是默认的并且很容易实现,但是对于 Firebase Cloud Functions,我找不到办法完成它。
我的代码如下:
from firebase_functions import https_fn
@https_fn.on_request(region="europe-west1")
def hello_world(req: https_fn.Request) -> https_fn.Response:
return https_fn.Response(f"Hello World")
但是如果我部署我的函数(
firebase deploy
),它始终是一个允许未经身份验证的调用的公共端点。
我该如何改变这个???
PS:我的大目标是一个函数,我只能从云 shell 调用它(用于管理内容),但我希望所有函数都在一个地方,所以我想在我的 firebase 项目中与我的其他云函数一起定义它
谢谢您的帮助:D
直到知道我找不到解决方案,我的目标是这样的:
而不是:
试试这个
@https_fn.on_request(region="europe-west1")
def hello_world(req: https_fn.Request) -> https_fn.Response:
# get Firebase ID token from the request headers and then verify it
id_token = req.headers.get('Authorization')
try:
decoded_token = auth.verify_id_token(id_token)
# Access granted to authenticated users
return https_fn.Response(f"Hello World")
except auth.InvalidIdTokenError:
# Access denied for invalid or missing ID token
return https_fn.Response("Access denied", status_code=401)