Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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

Lately, I have been working a lot with Google APIs on Android especially Analytics, AdSense and Tasks API.

I have seen some samples provided by Google where they use this statement to obtain a GoogleAccountCredential object

https://code.google.com/p/google-api-java-client/source/browse/tasks-android-sample/src/main/java/com/google/api/services/samples/tasks/android/TasksSample.java?repo=samples

credential = GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));

However, If I go through the documentation such as:
http://developer.android.com/google/auth/http-auth.html
http://developer.android.com/google/play-services/auth.html

Both of them mention the below method to be used for obtaining a token:
token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);

I am confused which one to use in which scenario and why. I have been using Method no. 1 successfully and without the need of persisting the token in preferences (I guess this is done by GoogleAccountCredential automatically)

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

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

question from:https://stackoverflow.com/questions/22142641/access-to-google-api-googleaccountcredential-usingoauth2-vs-googleauthutil-get

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...