1

I'm trying to figure out how to add a task using the Google tasks API. The docs leave a lot to investigate and I seen to be stuck. This is my code:

// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Tasks($client);
...
... get token etc...
...
// Create a task
$task = new Google_Service_Tasks_Task();
$task->setTitle("Call mum");
$task->setNotes("Inserted by app");
$task->setStatus("needsAction");
$task->setDue(new DateTime('2020-01-01T00:00:01.000Z'));
// constructor needs 4 params: service, serviceName, resourceName, resource
$res = new Google_Service_Tasks_Resource_Tasks('', '', '', '');
$res->insert($lastListID, $task);

When creating the new Google_Service_Tasks_Resource_Tasks (second-last line) I need to provide 4 parameters to the constructor: service, serviceName, resourceName, resource. I cant find any docs explaining the last three params. I use this class because it has an insert method and I think that's the one I need. The examples all stop at listing the tasks (works). I tried making sense of these docs:

I couldn't make sense of the actual classes in my vendor dir. Does anyone know how to tame this API?

Jacques-Guzel Heron
  • 2,480
  • 1
  • 7
  • 16
micksp
  • 123
  • 2
  • 15

1 Answers1

0

I understand that you want to create a task using the PHP Tasks API. Your approach is correct, you only need to use the constructor with three parameters instead of four. As we can see on the Google_Service_Tasks_Resource_Tasks documentation, the insert operation is defined as:

  /**
   * Creates a new task on the specified task list. (tasks.insert)
   *
   * @param string $tasklist Task list identifier.
   * @param Google_Service_Tasks_Task $postBody
   * @param array $optParams Optional parameters.
   *
   * @opt_param string parent Parent task identifier. If the task is created at
   * the top level, this parameter is omitted. Optional.
   * @opt_param string previous Previous sibling task identifier. If the task is
   * created at the first position among its siblings, this parameter is omitted.
   * Optional.
   * @return Google_Service_Tasks_Task
   */
  public function insert($tasklist, Google_Service_Tasks_Task $postBody, $optParams = array())
  {
    $params = array('tasklist' => $tasklist, 'postBody' => $postBody);
    $params = array_merge($params, $optParams);
    return $this->call('insert', array($params), "Google_Service_Tasks_Task");
  }

That comment define the three parameters as:

  1. Task list identifier. This is the id of the task list as defined on its resource.
  2. Content of the task object to be created, like the one on your code.
  3. Optional object containing two elements:
    1. Parent task identifier. This element is called id on the resource documentation. If the task has no parent task, this parameter can be omitted.
    2. Previous sibling task. This element is the same as the previous point, but makes reference to the previous element of the list. If the new element will be the first one between its sibling (or it will be the only one) this parameter can be omitted.

A working example, based on the variables of your code, will be:

$optParams = array("{PARENT TASK ID}", "{PREVIOUS TASK ID}");
$res = new Google_Service_Tasks_Resource_Tasks();
$res->insert($taskListID, $task, $optParams);

With this method you can create a task using your approach. Please, do not hesitate to ask for further clarification if you have any questions.

Jacques-Guzel Heron
  • 2,480
  • 1
  • 7
  • 16