无法使用用户身份验证令牌调用HTTP Cloud Function

问题描述 投票:0回答:1

我部署了一个可调用云函数,并想使用一些 POST 请求来测试它。以下是已部署函数的 Python 代码:

from firebase_functions import https_fn

@https_fn.on_call()
def my_function(req: https_fn.CallableRequest) -> Any:
    try:
        recId = req.data["recId"]
        return {"passedRecId": recId}
    except (ValueError, KeyError):
        raise https_fn.HttpsError(
            code=https_fn.FunctionsErrorCode.INVALID_ARGUMENT,
            message=("The function was called with incorrect arguments"),
        )

我为我的用户分配了所需的角色,如文档中所述,例如:

  • 云函数开发者
  • 云函数调用器 (
    cloudfunctions.functions.invoke
    )

当我尝试按照测试选项卡中的建议执行curl 命令时,

curl -m 130 -X POST https://us-central1-my-project.cloudfunctions.net/myFunction \
-H "Authorization: bearer $(gcloud auth print-identity-token)" \
-H "Content-Type: application/json" \
-d '{"data": {"recId": "6lKMe3XgbBQ6hvba5N1j"}}'

我收到以下错误:

警告:root:可调用请求验证失败:$[('身份验证令牌被拒绝。', {'app': 'MISSING', 'auth': 'INVALID', 'logging.googleapis.com/labels': {' firebase-log-type': '可调用请求验证'}})]"

当我在cloud shell中运行它时也失败。我多次浏览了文档,但无法理解为什么我的身份验证无效。是否还缺少我不知道的其他权限?任何指导将不胜感激。

python firebase google-cloud-functions gcloud
1个回答
0
投票

您正在编写 Firebase 可调用类型函数,但没有使用 Firebase CLI 部署它。不支持此操作。如果您要使用 Firebase 特定的库(Python 中的 firebase_functions)来编写 Cloud Functions 触发器,则需要使用 Firebase CLI 来部署它们。 gcloud 将无法工作。

您需要明确的另一件事:可调用函数不使用 GCP 服务帐户进行身份验证。他们使用 Firebase 身份验证用户帐户。可调用函数的预期用例是从使用 Firebase Auth 进行用户身份验证的 Web 或移动应用前端调用它们。 Firebase 提供的库自动使用当前登录用户的 ID 令牌来调用该函数。

© www.soinside.com 2019 - 2024. All rights reserved.