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