1

I'm trying to loop through task lists in order to generate a list of tasks using the Google Task PHP library.

I have:

  • Done all the credential stuff & can call the API
  • I can get task lists
  • A list of tasks for the respective task list output correctly using the ids generated from the point above & the tasklist parameter in Task API explorer

Where I'm stuck:

  • I'm not sure if I'm calling the 1) wrong method or 2) passing the wrong parameter to get a list of tasks for a respective tasklist id.

My code:

function getGcalTasks(){
  $client = $this->getGcalTaskClient();
  try {
    $service = new Google_Service_Tasks($client);
    $optParamLists = array(
      'maxResults' => 10,
    );
    $result_lists = $service->tasklists->listTasklists($optParamLists);
    if (
      is_array($result_lists->getItems())
      && count($result_lists->getItems())
    ) {
      foreach ($result_lists->getItems() as $tasklist) {
        $taskListId = trim($tasklist->getId());
        $taskListTitle = trim($tasklist->getTitle());
        if(
          $taskListId
        ){
          $optParamsTasks = array(
            // I've tried all of the below and still get: "Invalid task list ID",
            'id'         => $taskListId,
            'kind'       => 'tasks#taskList',
            'title'      => $taskListTitle,
            //'tasklist'   => $taskListId,
            //'taskList'   => $taskListId,
            //'tasklistId' => $taskListId,
            //'listName'   => $taskListTitle,
          );
          $result_tasks = $service->tasks->listTasks($optParamsTasks);
        }
      }
    }
  } catch (Exception $e) {
    log_message('error',$e->getMessage());
  }
}
Ryan Dorn
  • 679
  • 2
  • 8
  • 20

1 Answers1

1

Welp, I took a look a few minutes later and realized that listTasks() only accepts one parameter, the id. The code below is working for me:

function getGcalTasks(){
  $client = $this->getGcalTaskClient();
  $tasks = array();
  try {
    $service = new Google_Service_Tasks($client);
    $optParamLists = array(
      'maxResults' => 10,
    );
    $result_lists = $service->tasklists->listTasklists($optParamLists);
    if (
      is_array($result_lists->getItems())
      && count($result_lists->getItems())
    ) {
      foreach ($result_lists->getItems() as $tasklist) {
        $taskListId = trim($tasklist->getId());
        $taskListTitle = trim($tasklist->getTitle());
        if(
          $taskListId
        ){
          $optParamsTasks = array(
            'tasklist'   => $taskListId,
          );
          $result_tasks = $service->tasks->listTasks($taskListId);
          $tasks[]        = $result_tasks->getItems();
        }
      }
      return $tasks;
    }
  } catch (Exception $e) {
    log_message('error',$e->getMessage());
  }
}
Ryan Dorn
  • 679
  • 2
  • 8
  • 20