Solved

Google Task API Insert task

Posted on 2014-09-10
4
502 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
Comment Utility
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
Comment Utility
That worked. Thank you!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If your business is like most, chances are you still need to maintain a fax infrastructure for your staff. It’s hard to believe that a communication technology that was thriving in the mid-80s could still be an essential part of your team’s modern I…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
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)

763 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

12 Experts available now in Live!

Get 1:1 Help Now