Solved

Google Task API Insert task

Posted on 2014-09-10
4
537 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

832 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