Click here to see your recently viewed pages and most viewed pages.

Google Apps Tasks API: Instantiate a Client

You must instantiate a client to make requests to the API. All requests to the Google Tasks API require authentication.

The following code demonstrates how to configure your client and authenticate using OAuth 2.0 for native applications.







  public void setUp() throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    // The clientId and clientSecret are copied from the API Access tab on
    // the Google APIs Console
    String clientId = "YOUR_CLIENT_ID";
    String clientSecret = "YOUR_CLIENT_SECRET";

    // Or your redirect URL for web based applications.
    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    String scope = "";

    // Step 1: Authorize -->
    String authorizationUrl = new GoogleAuthorizationRequestUrl(clientId, redirectUrl, scope)

    // Point or redirect your user to the authorizationUrl.
    System.out.println("Go to the following link in your browser:");

    // Read the authorization code from the standard input stream.
    BufferedReader in = new BufferedReader(new InputStreamReader(;
    System.out.println("What is the authorization code?");
    String code = in.readLine();
    // End of Step 1 <--

    // Step 2: Exchange -->
    AccessTokenResponse response = new GoogleAuthorizationCodeGrant(httpTransport, jsonFactory,
        clientId, clientSecret, code, redirectUrl).execute();
    // End of Step 2 <--

    GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
        response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,

    Tasks service = new Tasks(httpTransport, accessProtectedResource, jsonFactory);


import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from import run

FLAGS = gflags.FLAGS

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(

# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('tasks.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='tasks', version='v1', http=http,


Edit the src/config.php file to put in your developer API information.

global $apiConfig;

$apiConfig = array(
    // Site name to show in Google's OAuth authentication screen
    'site_name' => '',

    // OAuth2 Setting, you can get these keys on the API Access tab on
    // the Google APIs Console
    'oauth2_client_id' => 'YOUR_CLIENT_ID',
    'oauth2_client_secret' => 'YOUR_CLIENT_SECRET',
    'oauth2_redirect_uri' => 'YOUR_REDIRECT_URL',

    // The developer key; you get this from the Google APIs Console
    'developer_key' => 'YOUR_DEVELOPER_KEY',

    // Which Authentication, Storage and HTTP IO classes to use.
    'authClass' => 'apiOAuth2',

    // Definition of service specific values like scopes, OAuth token URLs, etc
    'services' => array(
        'tasks' => array('scope' => ''),

Invoke the Tasks API service and authenticate:


require_once "../src/apiClient.php";
require_once "../src/contrib/apiTasksService.php";

$apiClient = new apiClient();
$service = new apiTasksService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;


using System;
using System.Threading;
using System.Threading.Tasks;

using Google;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Tasks.v1;
using Google.Apis.Tasks.v1.Data;
using Google.Apis.Services;

namespace Tasks.Sample
    class Program
        static void Main(string[] args)
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                    ClientId = "CLIENT_ID_HERE",
                    ClientSecret = "CLIENT_SECRET_HERE",
                new[] { TasksService.Scope.Tasks },

            // Create the service.
            var service = new TasksService(new BaseClientService.Initializer()
                HttpClientInitializer = credential,
                ApplicationName = "Tasks API Sample",