You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP Cloud Scheduler调用需认证的Gen2 Cloud Function认证失败问题

Cloud Scheduler调用需认证的Gen2 Cloud Function时认证失败

遇到的错误:

The request was not authenticated. Either allow unauthenticated invocations or set the proper Authorization header.

执行的操作步骤:

  • 创建Cloud Function:
gcloud functions deploy test-function \
--runtime=python310 \
--trigger-http \
--entry-point=start_script \
--region=europe-west3 \
--max-instances=1 \
--timeout=350s \
--memory=1GiB \
--service-account=973117053722-compute@developer.gserviceaccount.com \
--ingress-settings=all \
--no-allow-unauthenticated \
--gen2 \
--source=./path/to/dir
  • 部署Cloud Scheduler:
gcloud scheduler jobs create http test-scheduler \
--schedule="0 4 * * *" \
--http-method=POST \
--uri="https://europe-west3-propane-nomad-396712.cloudfunctions.net/test-function" \
--headers="Content-Type=application/json" \
--attempt-deadline=1800s \
--location='europe-west3' \
--oidc-service-account-email=cloud-scheduler@propane-nomad-396712.iam.gserviceaccount.com
  • 获取当前服务列表:
gcloud run services list --platform=managed
  • 基于上一步的地址为Cloud Function授予roles/run.invoker角色:
gcloud run services add-iam-policy-binding test-function \
    --member="serviceAccount:service-973117053722@gcf-admin-robot.iam.gserviceaccount.com" \
    --role="roles/run.invoker"

备注

  • Cloud Function使用默认Compute服务账号,为Cloud Scheduler创建了具备云函数调用权限的自定义服务账号。
  • 需要Cloud Function保持认证要求。
  • 最初仅执行前两步时调用失败,根据Cloud Function提示添加了第三步和第四步。

更新
尝试执行以下命令:

gcloud functions add-iam-policy-binding test-function \
    --member="serviceAccount:cloud-scheduler@propane-nomad-396712.iam.gserviceaccount.com" \
    --role="roles/cloudfunctions.invoker" \
    --region='europe-west3'

该命令不适用于Gen2云函数,gcloud提示需执行gcloud functions add-invoker-policy-binding命令,确认后调用成功。但重新部署后执行该命令:

gcloud functions add-invoker-policy-binding test-function \
    --member="serviceAccount:cloud-scheduler@propane-nomad-396712.iam.gserviceaccount.com" \
    --region='europe-west3'

命令执行成功,但触发Cloud Scheduler时仍出现相同的认证失败错误。我想知道gcloud后台实际执行的命令是什么?

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 13:50:33