biz*_*z84 7 firebase google-cloud-platform
我尝试从客户端应用程序调用可调用的云函数(已部署)并在 GCP 日志上收到此错误:
{
httpRequest: {9}
insertId: "647865c20002422d2d32b259"
labels: {1}
logName: "projects/faker-app-flutter-firebase-dev/logs/run.googleapis.com%2Frequests"
receiveTimestamp: "2023-06-01T09:32:50.154902339Z"
resource: {2}
severity: "WARNING"
spanId: "11982344486849947204"
textPayload: "The request was not authorized to invoke this service. Read more at https://cloud.google.com/run/docs/securing/authenticating Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#401"
timestamp: "2023-06-01T09:32:50.138090Z"
trace: "projects/faker-app-flutter-firebase-dev/traces/ddcb5a4df500af085b7a7f6f89a72ace"
traceSampled: true
}
Run Code Online (Sandbox Code Playgroud)
相同的功能可以在 Firebase 本地模拟器中正常工作,因此我认为这是与 IAM 和服务帐户相关的权限问题(我仍然不太了解 IAM 的工作原理)。
这是我的代码:
import * as admin from "firebase-admin"
import * as functions from "firebase-functions/v2"
import * as logger from "firebase-functions/logger";
// https://github.com/firebase/firebase-tools/issues/1532
if (admin.apps.length === 0) {
admin.initializeApp()
}
export const deleteAllUserJobs = functions.https.onCall(async (context: functions.https.CallableRequest) => {
const uid = context.auth?.uid
if (uid === undefined) {
throw new functions.https.HttpsError("unauthenticated", "You need to be authenticated to perform this action")
}
const firestore = admin.firestore()
const collectionRef = firestore.collection(`/users/${uid}/jobs`)
const collection = await collectionRef.get()
logger.debug(`Deleting ${collection.docs.length} docs at "/users/${uid}/jobs"`)
// transaction version
await firestore.runTransaction(async (transaction) => {
for (const doc of collection.docs) {
transaction.delete(firestore.doc(`/users/${uid}/jobs/${doc.id}`))
}
})
logger.debug(`Deleted ${collection.docs.length} docs at "/users/${uid}/jobs"`)
return {"success": true}
})
Run Code Online (Sandbox Code Playgroud)
该函数是使用 部署的firebase deploy --only functions,并且我确保客户端应用程序在用户已获得授权时调用此函数。
根据文档:
如果您在部署函数时遇到权限错误,请确保将适当的 IAM 角色分配给运行部署命令的用户。
该文档还链接到此页面,其中显示:
Firebase 权限的云功能
有关 Cloud Functions 权限的列表和说明,请参阅 IAM 文档。
请注意,功能的部署需要特定的权限配置,这些权限不包含在标准 Firebase 预定义角色中。要部署功能,请使用以下选项之一:
Run Code Online (Sandbox Code Playgroud)Delegate the deployment of functions to a project Owner. If you're deploying only non-HTTP functions, then a project Editor can deploy your functions. Delegate deployment of functions to a project member who has the following two roles: Cloud Functions Admin role (roles/cloudfunctions.admin) Service Account User role (roles/iam.serviceAccountUser) A project Owner can assign these roles to a project member using the Google Cloud Console or gcloud CLI. For detailed steps and有关此角色配置的安全影响,请参阅 IAM 文档。
但正如我所说,我可以成功部署该功能。当我尝试执行它时,我收到错误日志。
总之,我想做的事情非常基本:
当函数运行时,它失败并出现上述错误。
有什么建议吗?我需要设置特定的 IAM 角色吗?
打开https://console.cloud.google.com/iam-admin/<project_name>,找到您在 firebase 项目中使用的服务帐户并添加 Rol“Cloud Functions Invoker”。
就像管理员、编辑者或查看者角色是关于操作 GCP 上的函数(不允许您使用它),而调用者允许该帐户调用该函数。
考虑到您可能需要在 Firebase cli 中再次初始化 Firebase 项目,并且可能需要删除并再次上传云函数。
| 归档时间: |
|
| 查看次数: |
1638 次 |
| 最近记录: |