Cloud Task Creation : Error: 3 INVALID_ARGUMENT: Request contains an invalid argument

Viewed 1569

I'm following thist tutorial : https://cloud.google.com/tasks/docs/tutorial-gcf

To create a Task that would call a cloud function.

I've done quite some tries and still get this error:

enter image description here

If I change the body encoding to something else, I get another error about serialisation method.

It's likely not a permission issues, as I got some before and got rid of it.

The object which is pass to the createTask() is the following :

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"
  }
}

(or with body: base64 encoded json string.)

The code I use is the following :

'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}`, {parent:parent,task:task, 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
};

The lack of details in the error makes me hit a wall blind...

Any suggestions ?

2 Answers

My service account was missing a part...

it was

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

instead of

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

I left out the ".iam" during my numerous test to make it work.

For sure there's room for improvement in the error messages.

I had same problem. In your case I think there is not property scheduleTime into task param. To me, the scheduleTime.seconds was with a wrong value.

Related