Interacting with Google Tasks on Android

Since I’ve been playing with Google Tasks, I wanted to look at my task from my Android phone. Sadly, all the application I could find were using a lot more permissions than I like to give. Therefore, I decided to look into why all these permissions are needed. This will be a little tutorial on interacting with Google Tasks on Android.

Disclaimer: I have not yet published an application using this, so it is possible that something is missing. I will update this post as soon as I have more information (or remove this disclaimer if nothing more is needed).

Project Setup

There are two basic files that need modification in order to be able to use Google Tasks on Android.

The first files you need to modify is the AndroidManifest.xml file. You will need to add the android.permission.INTERNET by adding the following line:

This permission is used to allow your application to connect to the Internet.

The second file that needs modification is the app/build.gradle file. Adding the following lines to this file will add the needed libraries to your project’s dependencies:

Do note that I had to force the version of com.google.code.findbugs:jsr305 in order to fix a dependency issue.

Google API Setup

Configuring the Google Backend services for Android is a little bit different from doing so for desktop applications. I already did a quick tutorial on doing the configuration for Android here.

Handling the user’s credentials

In order to get the user’s credentials, you will need to ask for his permission. I went the easy route and asked directly for read/write access to Google Tasks, but you can get the list of scopes that can be used here. In order to be able to get the user’s token, you need to add the requestEmail permission.

Basically, the way the permission flow works is:

  1. Build a sign-in intent (with the desired permissions);
  2. Ask the system to fulfill the intent and wait for the response;
  3. Process the response

The code is quite straightforward:

Initializing the Google Tasks API

In order to initialize the Google Tasks API, you will need 3 things:

  • A JSONFactory
    I went with an instance of com.google.api.client.json.jackson2.JacksonFactory simply because it was there and worked
  • HttpTransport
    Depending on the Android version you are targeting, you must use a different class. Using AndroidHttp.newCompatibleTransport() makes this selection transparent.
  • The user’s credentials

Interacting with Google Tasks on Android

Once you got the Tasks Service in your hands, interacting with it is the same as doing so in Java. Since I’ve been using API 23, I can’t use streams like I used in my java example. Therefore, I did two little helpers ( TaskList findTaskList(TaskLists tasklists, String title)  and Task findTask(Tasks tasks, String title)).

Creating a task list

Finding a task list

Creating a task

Finding a task

Updating a task

Completing a task

Handling a large number of return values

Like any good online service, Google Tasks API handles a large number of values using pagination. By default, most APIs will return a maximum of 100 results. You are allowed to change this value, but in order to get good performance, you should not put it bigger.

Basically, the way to handle pagination is: do your normal call, check if you got a page token and if you do you know there are more results waiting. You can then call the API again with the page token in order to get the next result set:

Sources

How to access Google APIs and services using Android

I already did a post on how to access Google APIs and services using various language. It is now time for a quick tutorial on accessing Google APIs and services using Android. More precisely, it will look at the configuration that needs to be done on Google APIs Console in order to grant access to your Android application.

Disclaimer: I have not yet published an application using this, so it is possible that something is missing. I will update this post as soon as I have more information (or remove this disclaimer if nothing more is needed).

Basic setup

In my other post, I already went over the basic setup. You will need to follow two of the sections in the first post: Enable needed APIs and Setup OAuth Consent Screen. This will configure the basis of your Google project.


Gather project information

Before even going to the Google APIs Console, you will need to find two information from your Android Application:

  1. Your package name (can be found in the AndroidManifest.xml file of your application);
  2. The SHA1 fingerprint from your application

Assuming that you are using Gradle, you can run the android/signingReport task in your app context. The output of this task will be shown in your Gradle console. Looking at the output from that task, you’ll be able to find the SHA1 signature associated with your application.


Create credentials

With the previous information in hand, you can now go in the Google APIs console to allow your application to connect. In order to do so, you’ll need to go into the Credentials section, hit Create credentials and select OAuth Client ID.

The next page is the one where you will need all that information you gathered. The first thing you’ll need to do is select the type of application: Android. With this done, you will be able to select the remaining options:

  1. Name
    This is the name of your application, it is used only in the Google APIs console;
  2. Signing-certificate fingerprint
    This is the SHA1 signature you extracted from your project in the previous step;
  3. Package name
    This is the package name you extracted in the previous step;

Creating a new credential for Android

Creating a new credential for Android

Specifying the android credential information

Specifying the android credential information


All done! Your Android application should now be able to connect to the services.


Sources

Interacting with Google Tasks using Java

I already did a post giving an overview of interacting with Google Tasks using Ruby, but with the change in scope of my project, I now need to do the same in Java. Therefore, here is an overview of interacting with Google Tasks using Java. Do note that this probably won’t work on Android, it is meant to be used from Java Desktop editions.

Project Setup

Using gradle, the project setup is quite simple:

Google API Setup

In order to access Google Tasks using Java, you will need to use Google’s OAuth. Google’s OAuth uses what they call client secrets file. These files contain all the information needed to access the selected services.

To get a client secrets file, you can follow my other post.

Handling the user’s credentials

This project is my first Java project since a long time ago. I quickly remembered how painful it can be to play with the interface/abstract class hell. You have to instantiate 20 different things before calling anything and selecting the right implementation, or even simply finding an implementation, is sometimes tricky. Here, I selected the following implementations:

  • JsonFactory: com.google.api.client.json.jackson2.JacksonFactory, it was there and worked, so I used it;
  • HttpTransportcom.google.api.client.http.javanet.NetHttpTransport, according to the API’s documentation “NetHttpTransport is based on the HttpURLConnection built into the Java SDK, so it is normally the preferred choice“;
  • DataStoreFactorycom.google.api.client.util.store.FileDataStoreFactory, this implementation will store the tokens in a file (Ok … multiple files …). In my case, it will be in a directory called tokens in the current working directory;

Here is the authentication code I used. It will take care of caching the tokens, opening the browser if a new token is needed and getting the result token.

Initializing the Google Tasks API

In order to use the Google Tasks API, you need to build the service. Doing so requires most of the things that were initialized in the previous section:

Interacting with Google Tasks using Java

Creating a task list

Once you have all the boilerplate done for the initialization of the API and the service, creating a tasklist becomes quite easy:

Finding a task list

The Google Tasks API only allows you to search by tasklist ID, not a title. This ID is automatically generated and therefore not really easy to use. The easiest way to find a task list is to list all task lists and find the one you are looking for in there:

Creating a task

In order to create a task, you first need a tasklist. Once you got that, creating the task is easy enough:

Finding a task

Just like finding a tasklist, you can’t directly search by task title. Therefore, you need to go iterate over the full list to find the one you are looking for:

Updating a task

Once you have a task (and a tasklist) in your hands, updating most of its fields can be done directly on the Task instance. The following example updates the due date of a task:

Completing a task

Just like updating a task, completing it is quite easy: set its status field to completed:

Handling a large number of return values

Like any good online service, Google Tasks API handles a large number of values using pagination. By default, most APIs will return a maximum of 100 results. You are allowed to change this value, but in order to get good performance, you should not put it bigger.

Basically, the way to handle pagination is: do your normal call, check if you got a page token and if you do you know there are more results waiting. You can then call the API again with the page token in order to get the next result set:

 

Sources

Interacting with Google Tasks using Ruby

On the first iteration of a project of mine, I wanted to access Google Tasks using Ruby. Therefore, I did a little bit of tinkering with using the Google Tasks API using Ruby. In this post, I will go over your project’s setup, the Google backend setup and the basic usage of the API.

Project Setup

All of the Google Tasks APIs are basically REST endpoints. One could simply call all these HTTP endpoints manually, but this would be painful. I decided to use google-api-client to do all the heavy lifting. In order to include it in your project, simply add the following to your Gemfile:

Google API Setup

In order to access Google Tasks using Ruby, you will need to use Google’s OAuth. Google’s OAuth uses what they call client secrets file. These files contain all the information needed to access the selected services.

To get a client secrets file, you can follow my other post.

Handling the user’s credentials

The way the Google APIs work, an URL will be provided to you. Opening that URL and grabbing the authentication token is all on you. Therefore, there is three consideration when handling the user’s credential:

  1. Storing the credentials (so that you don’t ask access every time);
  2. Showing the consent webpage;
  3. Grabbing the authentication token;

For this quick test, I decided to use a file storage for the token. The FileTokenStore token store will simply use a YAML file to store the user’s tokens. To show the consent webpage, I went with launchy. This cross-platform library will handle opening URLs in the default browser and such. Finally, I decided to basically do nothing to grab the token: I expect the user to paste it on the command line.

Initializing the Google Tasks API

Now that you have the user’s credentials, you can initialize the API you want to use. In my case, this is the task API. In order to do so, simply create a new instance of the service you want to use (Google::Apis::TasksV1::TasksService) and set its authorization member to the credentials you got from the client.

Interacting with Google Tasks using Ruby

Creating a task list

The tasklist is basically a grouping of tasks. It is the root concept of Google Tasks. Creating one is quite straightforward:

Finding a task list

The Google Tasks API only allows you to search by tasklist ID, not a title. This ID is automatically generated and therefore not really easy to use. The easiest way to find a task list is to list all task lists and find the one you are looking for in there:

Creating a task

Creating a simple task, at the top of the task list is quite straightforward:

Finding a task

Just like finding a task list, finding a task by its title can’t be done directly through the API. In order to do so, you need to iterate over the tasks and check them:

Updating a task

Once you have your hand on the task you want to update, simply update the field you want to update and call update_task:

Completing a task

Completing a task is done by setting its status to “completed” and updating the task:

Handling a large number of return values

Google’s Task API uses pagination in order to handle a large number of return values. By default, a maximum of 100 return values will be returned by calls to list_tasklists and list_tasks. The return values of these two APIs will contain a next_page_token value. You can then use this token in a subsequent call to the API specifying the optional argument page_token in order to get another set of return values.

Here is a quick example of handling of pagination with the list_tasks call. Do note that in this example, I set the max_results value in the call so that I do not have to create 100+ tasks.

Sources

How to access Google APIs and services

For a side project of mine, I needed to access Google APIs, more precisely, I needed Google Tasks API access. This tutorial will show how to access google APIs and services.

The easiest way to access the various Google APIs is to use one of their language-specific wrappers. These will require you to provide a client secrets file. This file will contain all the information to access the services you enabled.  All the backend configuration is done through Google APIs Console.

Warning: It’s not quite clear if this file is safe to be shared with anyone (aka, checked into a public repository, distributed to clients, etc.). The way I personally see it is that this file allows clients to connect to the Google services you enable. Some of these services have a quota, others are simply paid. Therefore, anyone getting your file can easily go over your quota or get you billed quite a bit in a month. Basically, what I am saying is that I would not distribute this file to other people.

Enable needed APIs

After creating your project, the first thing you’ll need to do is select which APIs you want to use. In order to do so, simply go into the Dashboard section and hit the Enable APIs and Services button. The next page will allow you to search and select which APIs you want to enable. Once an API is selected, a page describing it and allowing you to enable the API will be shown. Check this page carefully, some APIs have quotas or are paid services.


Getting to the enable APIs page

Getting to the enable APIs page

Searching for APIs to enable

Searching for APIs to enable

Enabling an API

Enabling an API


Setup OAuth Consent Screen

The client secrets file is only one part of the OAuth process. It will be used to access your configuration, quotas, make you pay for services, etc. It is provided by the software/tool/website maker. The second part of the process is the client information. This will be provided by the user of the software/tool/website and will allow the maker to access some information about the user.

For example, a website could request access to the Google Drive content of a user. The APIs quotas that will be used are the ones from the owner of the website, but the information accessed will be the one of the user.

Before the website is allowed to access information about the client, the client must consent to share this information. Google requires you to customize this consent screen before it can be used. In order to do so, you will need to go to the Credentials section, then select the OAuth Consent Screen tab. In there, you will be able to customize quite a few information to make your consent screen nice and professional. The bare minimum is to select an email address and a product name. Both of these will be shown to the user when asking him to consent to the sharing his information.


Customizing the consent screen

Customizing the consent screen


Create credentials and download client secrets file

All the grunt work is now done for your project. The last piece of the puzzle is to create the credential and get the file. In order to create the credentials, you will need to go to the Credentials section and hit the Create Credentials button. In the submenu, you will need to select OAuth client ID. The next page will allow you to select the type of application. According to the Google documentation, there should be an Installed application option in there. That option would have been perfect for me (working on a command line tool here). In the end, I went with the only thing that made sense: Other.

Once your credential is created, simply go to the Credentials section, find the credential you want to use and hit the download icon. All done!


Creating a new credential

Creating a new credential

Specifying the credential information

Specifying the credential information

Downloading client secrets file

Downloading client secrets file


Sources