Google Apps Platform

Using 2-legged OAuth with Google Tasks API for Google Apps Marketplace applications

[# Some custom styles]

Nicolas Garnier
Nicolas Garnier profile | twitter
Google Developer Relations
June 2011

This article explains how you can use 2-legged OAuth 1.0 (2-LO) with the Google Tasks API if you are a Google Apps Marketplace applications.

Note: If you are a Google Apps domain administrator looking for how to use 2-LO, please read the article Using 2-legged OAuth with Google Tasks API for Google Apps domain administrators instead.

Contents

Get your Google Apps Marketplace application's OAuth credential

As a Google Apps Marketplace developer, you are using a Consumer Key and Secret associated with a Google Apps Marketplace Application. Simply grab your Consumer Key and your Consumer Secret from your application's listing in your vendor profile. You can find this at: Google Apps Marketplace > My Vendor Profile > Listings > [Application's listing] > View OAuth Consumer Key

Getting the Consumer Key and Secret of a Google Apps Marketplace application
Getting the Consumer Key and Secret of a Google Apps Marketplace applications

Your Consumer Key and Secret allows you to get access to the APIs that you specified in the manifest file of your application. This also gives you access to the data of the users of the domains that have installed your application and therefore to which an administrator has granted you access.

Please keep your Consumer Key and your Consumer Secret. You will need them later in the Using the client libraries section. Also note your Application ID as you will need it in the next section: Enable Tasks API access for your project in the APIs Console.

Enable Tasks API access for your project in the APIs Console

You need to enable the Tasks API in the APIs Console project that is linked to your Google Apps Marketplace application. To get there please go to the following URL: https://code.google.com/apis/console/marketplace?appId=<Application ID> You will need to replace <Application ID> by the Application ID number of your Google Apps Marketplace application (see section Get your Google Apps Marketplace application's OAuth credential):

In the All services page of your project, enable access to the Tasks API for your project by simply toggling the On/Off Tasks API switch to "On" in the All Services page.

Enabling the Google Tasks API for a project in the APIs Console
Enabling the Google Tasks API for a project in the APIs Console

Using the client libraries

Below are some very basic code samples using our Tasks API client libraries for multiple languages. These code samples show you in Java and Python how to authenticate to the service using 2-LO and run a basic query.

You should now have these four values that are required to authenticate using 2-LO on the Tasks API:

Code samples:

Python

# Initializing some objects
http = httplib2.Http()

# The 2-LO authorization section
credentials = TwoLeggedOAuthCredentials(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, '2-lo-python-sample/1.0')
http = credentials.authorize(http)
credentials.requestor = ACCOUNT_EMAIL

# Initializing the Tasks API service
service = build('tasks', 'v1', http=http)

# Performing first request: Getting the tasks lists
tasklists = service.tasklists().list().execute()

# Simply printing the title of each tasks lists
for tasklist in tasklists['items']:
  print tasklist['title']
    

Java

// Initializing some Objects
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();

// The 2-LO authorization section
OAuthHmacSigner signer = new OAuthHmacSigner();
signer.clientSharedSecret = OAUTH_CONSUMER_SECRET;

OAuthParameters oauthParameters = new OAuthParameters();
oauthParameters.version = "1";
oauthParameters.consumerKey = OAUTH_CONSUMER_KEY;
oauthParameters.signer = signer;
oauthParameters.signRequestsUsingAuthorizationHeader(httpTransport);

// Initializing the Tasks API service
Tasks service = new Tasks("2-lo-tasks-test/1.0", httpTransport, jsonFactory);

// Performing first request: Getting the tasks lists
List getTaskListsOperation = service.tasklists.list();
getTaskListsOperation.unknownFields.add("xoauth_requestor_id", ACCOUNT_EMAIL);
TaskLists taskLists = getTaskListsOperation.execute();

// Simply printing the title of each tasks lists
for (TaskList taskList : taskLists.items) {
  System.out.println(taskList.title);
}
    

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.