The problem was in the setdue()
Request contains an invalid argument - Google Task API
I want to implement Google Task API with laravel, I followed the documentation properly. I got the error above. I tried also google playground, Everything going fine, the problem is when I try to give the parameter of a task for a new task.
generated error -
{ "error": { "code": 400, "message": "Request contains an invalid argument.", "errors": [ { "message": "Request contains an invalid argument.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } }
I didn't provide an invalid argument. from the documentation, the parameter should be - Task list identifier which is a string, and the request body should be a task instance. I tried to test in Google playground to pass the task instance details in the request body, the same error was generated.
Can anyone come up with any idea?
my full controller code -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Google\Client as GoogleClient;
use Google\Service\Tasks;
use App\Models\User;
class Task extends Controller
{
public function createTask()
{
$googleClient = new GoogleClient();
$googleClient->setClientId(env('GOOGLE_CLIENT_ID'));
$googleClient->setClientSecret(env('GOOGLE_CLIENT_SECRET'));
$googleClient->setRedirectUri(env('GOOGLE_REDIRECT_URI'));
$user = User::find(auth()->user()->id);
$user_session = $user->users_sessions->last();
$accessToken = $user_session->access_token;
$googleClient->setAccessToken($accessToken);
$googleClient->setApplicationName('My App');
$googleClient->setScopes([Tasks::TASKS]);
$googleClient->setAccessType('offline');
$tasksService = new Tasks($googleClient);
$newTask = new \Google\Service\Tasks\Task();
$newTask->setTitle("Test Task");
$newTask->setDue("2023-05-01");
$newTask->setNotes("This is a test task.");
$newTask->setKind("tasks#task");
$searchStartDate = "2023-05-01";
$searchEndDate = date('Y-m-d', strtotime('+1 day', strtotime($searchStartDate)));
$taskListId = null;
$taskLists = $tasksService->tasklists->listTasklists();
foreach ($taskLists as $taskList) {
$tasks = $tasksService->tasks->listTasks($taskList->id);
foreach ($tasks as $task) {
$dueDate = $task->getDue();
if ($dueDate) {
$dueDateTimestamp = strtotime($dueDate);
if ($dueDateTimestamp >= strtotime($searchStartDate) && $dueDateTimestamp < strtotime($searchEndDate)) {
$taskListId = $taskList->id;
break 2; // break out of both loops
}
}
}
}
if (!$taskListId) {
$newList = new \Google\Service\Tasks\TaskList();
$newList->setTitle('my list1');
$createdList = $tasksService->tasklists->insert($newList);
$taskListId = $createdList->getId();
}
$existingTasks = $tasksService->tasks->listTasks($taskListId);
foreach ($existingTasks as $existingTask) {
$existingTaskDueDate = $existingTask->getDue();
if ($existingTaskDueDate == $newTask->getDue()) {
return response()->json([
'message' => 'There is a task conflict with the same due date.',
], 409);
}
}
//dd($newTask, $createdList, $tasksService->tasks);
$createdTask = $tasksService->tasks->insert($createdList->id, $newTask);
return response()->json($createdTask);
}
}
especially this line generated this error -
$createdTask = $tasksService->tasks->insert($createdList->id, $newTask);
Please or to participate in this conversation.