云任务创建:错误:3 INVALID_ARGUMENT:请求包含无效的参数

问题描述

我正在关注本教程:https://cloud.google.com/tasks/docs/tutorial-gcf

创建一个将调用云功能的任务。

我已经做了很多尝试,但仍然出现此错误:

enter image description here

如果将主体编码更改为其他内容,则会出现有关序列化方法的另一个错误。

这可能不是权限问题,因为我之前有一些问题并得到了解决。

传递给createTask()的对象如下:

task: {
  httpRequest: {
    url: "https://europe-west1-project_id.cloudfunctions.net/FunctionName"
    httpMethod: "POST"
    oidcToken: {
      serviceAccountEmail: "cf-targetFunctionSA@project_id.gserviceaccount.com"
    }
  body: ""
  headers: {
    Content-Type: "application/json"
  }
}

(或带有主体:base64编码的json字符串。)

我使用的代码如下:

'use strict';
const common            = require('./common');
const {v2beta3}         = require('@google-cloud/tasks');
const cloudTasksClient  = new v2beta3.CloudTasksClient();

let projectName = common.getProjectName();
let location    = "europe-west3";
let queue       = "compute-stats-on-mysql";
const parent    = cloudTasksClient.queuePath(projectName,location,queue);

async function createTask(url,serviceAccount,data)
{
  const dataBuffer  = Buffer.from(JSON.stringify(data)).toString('base64');
  const task = {
    httpRequest: {
      httpMethod: 'POST',url:url,oidcToken: {
        serviceAccountEmail: serviceAccount,},headers: {
        'Content-Type': 'application/json',body:dataBuffer,};


  try
  {
    // Send create task request.
    common.logDebug(`Before creating task`,{parent:parent,task:task,data:data});
    const [response] = await cloudTasksClient.createTask({parent,task});
    common.logDebug(`Created task ${response.name}`,response:response,data:data});
    return response;
  }
  catch (error)
  {
    // Construct error for Stackdriver Error Reporting
    console.error("error while creating tasks",error);
  }
}

module.exports = {
  createTask : createTask,cloudTasksClient:cloudTasksClient
};

错误中缺少细节,使我无所适从...

有什么建议吗?

解决方法

我的服务帐户缺少一部分...

"cf-"+functionName+"@"+projectName+".gserviceaccount.com";

代替

"cf-"+functionName+"@"+projectName+".iam.gserviceaccount.com";

在多次测试中,我省略了“ .iam”以使其正常工作。

确保错误消息中仍有改进的空间。

,

我遇到了同样的问题。在您的情况下,我认为任务参数中没有属性 scheduleTime 。 对我来说,scheduleTime.seconds 的值是错误的。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...