Since we launched the Google Tasks API we have received a warm welcome from the developer community on the public forum, and a lot of question on how to make it work with OAuth 2.0 on Android. Let's see how this can be done.
Android has its own native authorization flow and its own way of handling Google accounts as you can register them on your device. Since Android 2.0, the
AccountManager
manages the accounts that you have registered - the ones that are listed under
Settings > Accounts & sync. Specifically, it handles the authorization flow and can generate authorization tokens that are required to access data using APIs.
Accounts registered in your Android environment
You can use the
AccountManager
to get the Google account you want to access the Tasks for. Though as the
AccountManager
also manages accounts from other vendors you can simply instantiate a
GoogleAccountManager
which is provided in the
Google APIs Client library for Java and only handles Google accounts:
GoogleAccountManager googleAccountManager = new GoogleAccountManager(
activity);
Account[] accounts = accountManager.getAccounts();
If more than one Google accounts are available on the Android device you should prompt the user for the account he wishes to use through a dialog.
Now that the user has chosen an account (or if there was only 1 Google account) you can ask the
AccountManager
to issue an authorization token for the Tasks API. This is done by calling the
AccountManager.getAuthToken()
method. The
AccountManager
will take care of contacting the Google APIs authorization endpoint and run the
AccountManagerCallback
that you have defined in the method call when it has retrieved an authorization token:
googleAccountManager.manager.getAuthToken(account, AUTH_TOKEN_TYPE, null,
activity, new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> future) {
try {
// If the user has authorized your application to use the tasks API
// a token is available.
String token = future.getResult().getString(
AccountManager.KEY_AUTHTOKEN);
// Now you can use the Tasks API...
useTasksAPI(token);
} catch (OperationCanceledException e) {
// TODO: The user has denied you access to the API, you
// should handle that
} catch (Exception e) {
handleException(e);
}
}
}, null);
The Android
AccountManager
has support for OAuth 2.0. A user friendly
AUTH_TOKEN_TYPE
exists for the Tasks API which will make the
AccountManager
return an OAuth 2.0 access token:
String AUTH_TOKEN_TYPE = ”Manage your tasks”;
During the
AccountManager.getAuthToken()
call the
AccountManager
will check if your application has been authorized to access the Tasks API. If your application has not yet been authorized the user will be presented with an authorization dialog so that they can
Allow or
Deny your application to use the Tasks API on their account.
Authorization dialog
Another piece of getting the Tasks API to work using OAuth 2.0 on Android is that you also need to specify an
API Key when making calls to the Tasks API. The
API Key is mandatory as it identifies your application and therefore allows the API to deduct quota and use the quota rules defined for your project. You can get it from the
Google APIs Console at
API Access > Simple API Access > API Key. You need to specify the
API Key on your Tasks service Object:
useTasksAPI(String accessToken) {
// Setting up the Tasks API Service
HttpTransport transport = AndroidHttp.newCompatibleTransport();
AccessProtectedResource accessProtectedResource =
new GoogleAccessProtectedResource(accessToken);
Tasks service = new Tasks(transport, accessProtectedResource,
new JacksonFactory());
service.setKey(INSERT_YOUR_API_KEY);
service.setApplicationName("Google-TasksSample/1.0");
// TODO: now use the service to query the Tasks API
}
At this point you should have a fully setup Tasks API
service
Object which you can use to query the API as per the
Tasks API developer’s Guide.
If you would like to get more tips and learn more about getting the Google Tasks API and OAuth 2.0 working on Android please have a look at our newly published
article.
Also we recently added a new
sample to the Google APIs Client Library for Java sample repository to help you
getting started with the Tasks API and OAuth 2.0 on Android.
| Nicolas Garnier profile | twitter | events
Nicolas joined Google’s Developer Relations in 2008. Since then he's worked on commerce oriented products such as Google Checkout and Google Base. Currently, he is working on Google Apps with a focus on the Google Calendar API, the Google Contacts API, and the Tasks API. Before joining Google, Nicolas worked at Airbus and at the French Space Agency where he built web applications for scientific researchers.
|
Want to weigh in on this topic? Discuss on Buzz