在firebase函数中创建Google Cloud Task [英] creating Google Cloud Task in a firebase function

查看:192
本文介绍了在firebase函数中创建Google Cloud Task的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用尝试创建任务的firebase HTTP函数时,firebase控制台函数日志中出现错误.

I'm getting an error in the firebase console functions log when calling a firebase HTTP function that tries to create a task.

错误:7 PERMISSION_DENIED:主体(用户或服务帐户)缺少IAM权限"cloudtasks.tasks.create";对于资源"projects/my-gcloud-project-id/locations/us-central1/queues/myqueuename"; (或者资源可能不存在).

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&位置与Firebase ID&位置?

Maybe I'm confused between the gcloud id & location versus the firebase id & location?

我通过运行gcloud --project my-gcloud-project-id tasks locations list

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

Or maybe somehow I need to set up permissions?

我的代码:



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任务,您必须添加正确的

In order to create a Google Task you have to add the correct permissions on IAM, in this case as the error message is showing, you have to add the cloudtasks.tasks.create permission to the service account that is invoking the Cloud Function.

这可以通过以下方法完成:进入Cloud Console,然后进入IAM,搜索服务帐户

This can be done by going inside the Cloud Console and then into IAM, search for the service account usually is something like service-project-number@gcf-admin-robot.iam.gserviceaccount.com (update: it was my-project-id@appspot.gserviceaccount.com) and add the required permission, if you have a role based permissions Cloud Tasks Enqueuer should be enough to create the tasks.

这篇关于在firebase函数中创建Google Cloud Task的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆