Quickwork
Published in

Quickwork

How to use Google Task API

Google Tasks is an easy way to list down your to-dos, track the status, and know your progress. It works with Gmail and Google Task to keep you alerted and to schedule your tasks precise to the time.

Option 1: Google Task Official Client Libraries

Google provides official libraries across many popular languages like Java, Node.js, Python, PHP and more. A full list of available client libraries can be found on the Google Task documentation download page here. This may require more installation steps

Here’s an example of using the Google Task official Node.js client library:

Run the following commands to install the libraries using npm:

npm install googleapis@39 --save

If you are a developer with control over your environment (and especially can install new libraries), the official libraries will make it far quicker to start using the Google Task API.If you need more complex logging, or already have powerful HTTP client integration, you might prefer the Native HTTP APIs.

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/tasks.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Tasks API.
authorize(JSON.parse(content), listTaskLists);
});

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}

/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}

/**
* Lists the user's first 10 task lists.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listTaskLists(auth) {
const service = google.tasks({version: 'v1', auth});
service.tasklists.list({
maxResults: 10,
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
const taskLists = res.data.items;
if (taskLists) {
console.log('Task lists:');
taskLists.forEach((taskList) => {
console.log(`${taskList.title} (${taskList.id})`);
});
} else {
console.log('No task lists found.');
}
});
}

Run the sample using the following command:

node .

Option 2: Native Google Task HTTP APIs

Most programming languages provide HTTP clients that you can use to make your own HTTP calls to the API. In fact, the official client libraries often use them behind the scenes. You’ll have to set your own API key, headers, and other HTTP settings which can be cumbersome, but usually that is a very routine task. Once you write code to make a single API call, you can reuse it for later API calls.

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');

// If modifying these scopes, delete token.json.
const SCOPES = ['https://www.googleapis.com/auth/tasks.readonly'];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_PATH = 'token.json';

// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Tasks API.
authorize(JSON.parse(content), listTaskLists);
});

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
* @param {Object} credentials The authorization client credentials.
* @param {function} callback The callback to call with the authorized client.
*/
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}

/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getNewToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}

/**
* Lists the user's first 10 task lists.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listTaskLists(auth) {
const service = google.tasks({version: 'v1', auth});
service.tasklists.list({
maxResults: 10,
}, (err, res) => {
if (err) return console.error('The API returned an error: ' + err);
const taskLists = res.data.items;
if (taskLists) {
console.log('Task lists:');
taskLists.forEach((taskList) => {
console.log(`${taskList.title} (${taskList.id})`);
});
} else {
console.log('No task lists found.');
}
});
}

Here’s an example of using Google Task’s native HTTP API in Node.js using the popular requests library, it grabs a list of tasks in your account.

If you are a developer and are comfortable with your programming language’s standard library HTTP client, or have a favorite HTTP client in mind, the native HTTP API might provide a better experience. If you have other special needs like logging, HTTP proxies, and asynchronousness, the native HTTP API may be the only option that lets you customize Google Tasks API calls to work the way you need.

Option 3: Quickwork

Quickwork | Tutorial: Create Google Tasks From New Gmail

Quickwork | Tutorial: Update Old Tasks In Google Tasks When New Event Is Created In Google Task

Quickwork | Tutorial: Task Added To Google Tasks Will Add A New Row In Sheets

Quickwork | Tutorial: Add New Starred Gmail Emails To Google Tasks

Quickwork | Tutorial: Add New Google Task When We Receive New Email In Outlook

Quickwork | Tutorial: New Event Is Created Via Google Task Will Create A Task Via Google Tasks

Some Pre-built journeys using Google Task Application:

Create a new task in Google Tasks when a labelled email is received via Gmail

Create a Google Task when a new order is placed on Shopify

Create a new task in Google Tasks when a new DocuSign envelope is received

Where to go next with the Google Task API?

We recommend going to the Official Google Task API Reference to see all the endpoints you might want to use. You can integrate Google Tasks with 100+ applications with a zero-code drag and drop Journey builder within a matter of minutes.

Quickwork is a no-code, API-based, enterprise grade, SaaS platform with 1,000s of pre-integrated apps to automate various business and consumer workflows.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Team Quickwork

The #1 integration and automation platform chosen by enterprises to build workflows, publish APIs, and manage conversations.