Access to Google API - Googleaccountcredential.Usingoauth2 VS Googleauthutil.Gettoken()

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.

In a nutshell what's the difference from using OAuth2 request getAuthToken and getToken

I didn't know about using Google Play services for OAuth 2.0 authentication, but after taking a quick look at it, it looks pretty interesting and I think it's something I could prefer to use over the AccountManager.getAuthToken.

Major differences

AccountManager.getAuthToken

Pro:

  • Can be used for all Android 2.0 devices and newer.
  • Is built in to Android and doesn't require any separate SDK.
  • Can be used for all types of accounts that has an authenticator, not only Google.

Con:

  • Returns a token that may have expired so you always have to invalidate the token and request it again to make sure you have a valid token.
  • Requires the permissions GET_ACCOUNTS and USE_CREDENTIALS.
  • Challenge screen is not user friendly for Android 2.*

GoogleAuthUtil.getToken

Pro:

  • Always returns a valid token.
  • Only requires the permission GET_ACCOUNTS
  • User friendly challenge screen.
  • Recommended by Google

Con:

  • Require Android 2.2 and that the device have Google Play
  • Require that you download and include the Google Play services SDK in your app.
  • You need to register your app in the Google API Console
  • Can "only" be used for Google services that uses OAuth 2.0

Challenge screen comparison

AccountManager.getAuthToken Challenge screen on Gingerbread and Ice Cream Sandwich

getAuthToken challenge screen for Gingerbread
getAuthToken challenge screen for Ice cream sandwich

GoogleAuthUtil.getToken Challenge screen

getToken challenge screen
getToken challenge screen, more details

Summary

Since the GoogleAuthUtil approach has a much user friendlier challenge screen and requires less permissions at install time I would definitely use this approach instead of the AccountManager.getAuthToken approach whenever I can. Since you always get a valid token and don't have to hassle with invalidating the token it should make the code simpler as well.

Best practice in storing and using a OAuth2 Token in Android?

How are you retrieving the token or doing Authorization?

If you are using GoogleAuthUtil or GoogleAccountCredential API's, the persistance of token is automatically managed by API's themselves.

You should not be required to do it manually.

See this question on which one to use:

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

Google Drive API - the name must not be empty: null (But I had passed valid account name to GoogleAccountCredential)

It looks like the Drive API Client Library for Java calls into GoogleAuthUtil.getToken(), which requires the GET_ACCOUNTS permission. You need to have that permission in your manifest and request it at runtime, as appropriate.

UPDATE:
With Google Drive REST v3 API, GET_ACCOUNTS permission is not required. Instead, Email permission is required. You can ask for it by calling GoogleSignInOptions.Builder.requestEmail.



Related Topics



Leave a reply



Submit