Access to Google API – GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()

The Google APIs Client Library for Java is as the name suggests a library for accessing Google APIs and it is available for several platforms such as Java (in general) and Android while the Google Play Services and GoogleAuthUtil is only available on Android.

By looking at the wiki page of the project it is difficult to understand how Google APIs Client Library relates to GoogleAuthUtil since the wiki suggests that the AccountManager is used for handling Google accounts and it doesn’t really mention GoogleAuthUtil at all.

However if you dig into the code and their issue tracker a bit you can see that the tasks sample you linked actually uses GoogleAuthUtil since version 1.12.0 of the Google APIs Client Library when support for GoogleAuthUtil was added.

The wiki is probably mention the AccountManager instead of GoogleAuthUtil since that was the way to do OAuth2 authentication before GoogleAuthUtil was available and because that part of the wiki has not been updated yet.

For more information on the differences between the AccountManager and GoogleAuthUtil please see: In a nutshell what’s the difference from using OAuth2 request getAuthToken and getToken

In short Google APIs Client Library is a cross platform library for interacting with Google’s services and the Android version is implemented by using GoogleAuthUtil.

Can anyone tell me why would anyone use the first method as opposed to second ?

Reasons for using Google APIs Client Library

  • If you are developing for some other platform than Android you can not use GoogleAuthUtil as it is an Android specific library.
  • If you are developing a cross platform application you can use the Google APIs Client Library in your shared code for for both Android and other platforms.
  • If you interact a lot with many of Google’s services this library may make things easier for you.
  • If you are already using this and it works as wanted there isn’t really any drawback to continue using it as it is a wrapper for GoogleAuthUtil so you get all the advantages of GoogleAuthUtil compared to using the AccountManager or some other library based on the AccountManager.

Reasons for using GoogleAuthUtil

  • Using this requires no other libraries or external dependencies than the Google Play Services
  • Your app’s footprint should be smaller since you don’t have to include additional libraries.
  • If your interaction with Google is limited it might be easier to just use the GoogleAuthUtil directly instead of going trough another library.
  • GoogleAuthUtil shouldn’t be that hard to use as it is, so using a library that wraps around it to simplify it might not be that much easier to use.

I am confused which one to use in which scenario and why. I have been using Method no. 1 successfully …

If you are using the Google APIs Client Library and it works fine for you I don’t see any reason why you shouldn’t continue using it.

However if I would create an Android (only) application that needed to interact with Google’s services I would probably use GoogleAuthUtil directly.

… without the need of persisting the token in preferences (I guess this is done by GoogleAccountCredential automatically)

Yes I this is automatically handled by GoogleAuthUtil which is in turn used by GoogleAccountCredential.

How can I access the auth token in the first method ?

You should be able to call the method getToken() on the GoogleAccountCredential object.

Leave a Comment