Solved

Google Task API Insert task

Posted on 2014-09-10
4
519 Views
Last Modified: 2014-09-16
Can anybody write me an example to use the function testInsertTask()
at here: (This contains the offical library of Google task api)
https://github.com/google/google-api-php-client/blob/master/tests/tasks/TasksTest.php


I have managed to LIST THE TASKS with the below script. But cannot insert, I have tried something at the very below

<?php


include_once "google-api-php-client-master/examples/templates/base.php";


set_include_path("google-api-php-client-master/src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Tasks.php';
require_once 'Google/Service/Calendar.php';

/************************************************
  ATTENTION: Fill in these values! Make sure
  the redirect URI is to this page, 
 ************************************************/
$client_id = '72327291fffffffffffeusercontent.com';
 $client_secret = 'ssssssssssssssss';
 $redirect_uri = 'dddddddddd';


/************************************************
  Make an API request on behalf of a user. In
  this case we need to have a valid OAuth 2.0
  token for the user, so we need to send them
  through a login flow. To do this we need some
  information from our API console project.
 ************************************************/
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/tasks");
$client->addScope("https://www.googleapis.com/auth/calendar");

/************************************************
  We are going to create  services, 
 ************************************************/
$task_service = new Google_Service_Tasks($client);
$calendar_service = new Google_Service_Calendar($client);


/************************************************
  Boilerplate auth management - see
  user-example.php for details.
 ************************************************/
if (isset($_REQUEST['logout'])) {
  unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

 if($rest['TOKEN1']) {

  $_SESSION['access_token']=$rest['TOKEN1'];		
  $client->setAccessToken($_SESSION['access_token']);
	

}else {
  $authUrl = $client->createAuthUrl();
  header("Location:".$authUrl);
}



/************************************************
  If we're signed in, retrieve 
 ************************************************/
if ($client->getAccessToken()) {
  $_SESSION['access_token'] = $client->getAccessToken();


  
	$tasks = $task_service->tasks->listTasks('@default');
    echo "<h3>Results Of Task List:</h3>";
    foreach($tasks->getItems() as $task) {
	echo $task->getTitle();
	}

 
$task = $task_service->tasks->__construct();
$task->setTitle('New Task');
$task->setNotes('Please complete me');
$task->setDue(new TaskDateTime('2010-10-15T12:00:00.000Z'));

$result = $task_service->insertTasks('@default', $task);
echo $result->getId();


  
  
  
 }

    

    
 ?>

Open in new window

0
Comment
Question by:myyis
4 Comments
 
LVL 14

Accepted Solution

by:
BlueCompute earned 500 total points
ID: 40325085
Your code's become mangled somewhere along the line; does your actual code say:
task->setTitle('New Task');

Open in new window

Or does it say:
task->setTitle('New Task');

Open in new window

?

I'm assuming that's just an error that's been introduced when you copy-pasted and have corrected it in the version I have.

I'm also not clear on why you're trying to actually call the TasksTest.php samples - they're intended as demonstrations of how to use the API, they don't form part of it. What you should be doing is basing your code on the code in the samples - a cursory examination suggests to create a new task and add it to a list you do:
$tasks = $this->taskService->tasks; // Get a handle to our tasks
$task = new Google_Service_Tasks_Task(); // Create a new task object
$task->title = "New Task"; //Set the title
$task->notes = "Please complete me"; //Set the notes
$task->due = (new TaskDateTime('2010-10-15T12:00:00.000Z')); //Add a due date
$tasks->insert('@default', $task) // Insert the task into the default tasklist

Open in new window

0
 

Author Closing Comment

by:myyis
ID: 40325114
That worked. Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now