How do you use Google tasks API from an Android app?

1.3k views Asked by At

I'd like to be able to create a custom app that uses Google tasks. How would I go about using Google tasks API from an Android app?

In other words, how do I make an API call and use the response?

I found a tutorial located here that was helpful.

1

There are 1 answers

0
Alex Baker On

Register at cloud.google.com, enable the Tasks API, and create android authorization credentials

Add to your AndroidManifest.xml:

<uses-permission android:name="android.permission.GET_ACCOUNTS" android:maxSdkVersion="25"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS" android:maxSdkVersion="22" />

Add to your build.gradle dependencies:

implementation 'com.google.apis:google-api-services-tasks:v1-rev58-1.25.0'
implementation 'com.google.api-client:google-api-client-android:1.28.0'

Use AccountManager.newChooseAccountIntent to get an account, then:

String token = AccountManager.get(context)
    .getAuthToken(<android.accounts.Account>, "oauth2:" + TasksScopes.TASKS, new Bundle(), activity, null, null)
    .getResult()
    .getString(AccountManager.KEY_AUTHTOKEN);
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
Tasks service = new Tasks.Builder(new NetHttpTransport(), new JacksonFactory(), credential)
    .setApplicationName("app name")
    .build();

Now you can use the Tasks API, e.g. service.tasklists().list().execute()