gpt4 book ai didi

node.js - 在 firebase 函数中创建 Google Cloud Task

转载 作者:行者123 更新时间:2023-12-05 01:11:23 30 4
gpt4 key购买 nike

在调用试图创建任务的 firebase HTTP 函数时,我在 firebase 控制台函数日志中遇到错误。

Error: 7 PERMISSION_DENIED: The principal (user or service account) lacks IAM permission "cloudtasks.tasks.create" for the resource "projects/my-gcloud-project-id/locations/us-central1/queues/myqueuename" (or the resource may not exist).

也许我在 gcloud id & location 与 firebase id & location 之间感到困惑?

编辑:我已通过运行 gcloud --project my-gcloud-project-id tasks locations list

确认我的位置是 us-central1

或者也许我需要设置权限?

我的代码:



const functions = require('firebase-functions');
const { CloudTasksClient } = require('@google-cloud/tasks')

const projectId = 'my-firebase-project-id';
const location = 'us-central1'
const queue = 'myqueuename'

exports.onFormSubmit = functions.https.onRequest(async (req, res) => {
const tasksClient = new CloudTasksClient()
const queuePath = tasksClient.queuePath('my-gcloud-project-id', location, queue);

const url = `https://google.com/` // edited for stack overflow
const delaySeconds = 5;
console.log('delaying for ', delaySeconds, ' seconds');

const task = {
httpRequest: {
httpMethod: 'POST',
url,
body: '',
headers: {
'Content-Type': 'application/json',
},
},
scheduleTime: {
seconds: delaySeconds
}
}

const [ response ] = await tasksClient.createTask({ parent: queuePath, task })

console.log('task name', response.name);
});

最佳答案

为了创建 Google 任务,您必须添加正确的 permissions在 IAM 上,在这种情况下,如错误消息所示,您必须将 cloudtasks.tasks.create 权限添加到调用 Cloud Function 的服务帐户。

这可以通过进入云控制台然后进入 IAM 来完成,搜索服务帐户 usually类似于 service-project-number@gcf-admin-robot.iam.gserviceaccount.com(更新:它是 my-project-id@appspot。 gserviceaccount.com) 并添加所需的权限,如果您具有基于角色的权限,Cloud Tasks Enqueuer 应该足以创建任务。

关于node.js - 在 firebase 函数中创建 Google Cloud Task,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63269193/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com