Android 访问 Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22142641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:33:26  来源:igfitidea点击:

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

androidoauth-2.0google-apigoogle-api-java-client

提问by Madhur Ahuja

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

最近,我一直在 Android 上使用 Google API,尤其是 Analytics、AdSense 和 Tasks API。

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

我看过谷歌提供的一些示例,他们使用此语句来获取GoogleAccountCredential对象

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

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));

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

但是,如果我浏览文档,例如:
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);

他们都提到了以下用于获取令牌的方法:
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 成功,无需在首选项中保留令牌(我猜这是由 GoogleAccountCredential 自动完成的)

  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 ?

  1. 谁能告诉我为什么有人会使用第一种方法而不是第二种方法?

  2. 如何在第一种方法中访问身份验证令牌?

回答by nibarius

The Google APIs Client Library for Javais 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 Servicesand GoogleAuthUtilis only available on Android.

适用于 JavaGoogle API 客户端库顾名思义是一个用于访问 Google API 的库,它可用于多种平台,例如 Java(一般)和 Android,而Google Play 服务GoogleAuthUtil仅可用于 Android。

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

通过查看项目的wiki 页面,很难理解 Google API 客户端库与 GoogleAuthUtil 的关系,因为 wiki 建议AccountManager用于处理 Google 帐户,而它根本没有真正提到 GoogleAuthUtil。

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

然而,如果你深入到代码和问题跟踪了一下,你可以看到任务样你真正链接使用GoogleAuthUtil自1.12.0版本时是为GoogleAuthUtil支持谷歌API客户端库的补充

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.

wiki 可能提到 AccountManager 而不是 GoogleAuthUtil,因为这是在 GoogleAuthUtil 可用之前进行 OAuth2 身份验证的方式,并且因为 wiki 的那部分尚未更新。

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

有关 AccountManager 和 GoogleAuthUtil 之间差异的更多信息,请参阅:简而言之,与使用 OAuth2 请求 getAuthToken 和 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.

简而言之,Google APIs Client Library 是一个用于与 Google 服务交互的跨平台库,Android 版本是通过使用 GoogleAuthUtil 实现的。

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

谁能告诉我为什么有人会使用第一种方法而不是第二种方法?

Reasons for using Google APIs Client Library

使用 Google API 客户端库的原因

  • 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.
  • 如果您正在为 Android 以外的其他平台进行开发,则不能使用 GoogleAuthUtil,因为它是 Android 特定的库。
  • 如果您正在开发跨平台应用程序,您可以在 Android 和其他平台的共享代码中使用 Google API 客户端库。
  • 如果您与 Google 的许多服务进行了大量互动,这个库可能会让您更轻松。
  • 如果您已经在使用它并且它可以正常工作,那么继续使用它并没有任何缺点,因为它是 GoogleAuthUtil 的包装器,因此与使用 AccountManager 或其他一些基于 AccountManager 的库相比,您可以获得 GoogleAuthUtil 的所有优势.

Reasons for using GoogleAuthUtil

使用 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.
  • 使用它不需要除了 Google Play 服务之外的其他库或外部依赖项
  • 您的应用程序的占用空间应该更小,因为您不必包含额外的库。
  • 如果您与 Google 的交互受到限制,直接使用 GoogleAuthUtil 而不是通过另一个库可能会更容易。
  • GoogleAuthUtil 不应该像现在这样难以使用,因此使用环绕它的库来简化它可能不会那么容易使用。

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

我很困惑在哪种情况下使用哪个以及为什么。我一直在使用方法号。1 成功...

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.

如果您正在使用 Google API 客户端库并且它对您工作正常,我认为您不应该继续使用它的任何理由。

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

但是,如果我要创建一个需要与 Google 服务交互的 Android(仅限)应用程序,我可能会直接使用 GoogleAuthUtil。

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

...无需在首选项中保留令牌(我猜这是由 GoogleAccountCredential 自动完成的)

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

是的,这是由 GoogleAuthUtil 自动处理的,而 GoogleAuthUtil 又由 GoogleAccountCredential 使用。

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

如何在第一种方法中访问身份验证令牌?

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

您应该能够在对象上调用getToken()方法GoogleAccountCredential

回答by nightlytrails

Google Play Services client libraryis written specifically for Android devices to offer a seamless integration with individual Google services and a consistent user interface to obtain authorization from users to access these services with their credentials.

Google Play Services client library专为 Android 设备编写,以提供与各个 Google 服务的无缝集成和一致的用户界面,以获取用户授权以使用其凭据访问这些服务。

Google APIs Client Library for Javais a generic library to access Google Services from all application types (web, installed, or Android application).

Google APIs Client Library for Java是一个通用库,用于从所有应用程序类型(网络、已安装或 Android 应用程序)访问 Google 服务。

Coming back to Android, if the Google API you want to use is not included in the Google Play services library, you can connect using the appropriate REST API, but you must obtain an OAuth 2.0 token.

回到 Android,如果您要使用的 Google API 未包含在 中Google Play services library,您可以使用适当的 REST API 进行连接,但您必须获得 OAuth 2.0 令牌。

To obtain a token, you can either -

要获得令牌,您可以 -

  • directly use the OAuth 2.0 library from Google APIs Client Library for Java (not preferred for android)

  • or leverage the authorization portion of the Google Play services library using GoogleAuthUtiland AccountPicker. Read Authorizing with Google for REST APIs.(GoogleAuthUtil.getToken()caches and manages token expiry and refresh itself. However, in case of network errors/server load, you might need to use an exponential back-off algorithm before retrying for the token so to not flood the server with requests.)

  • or use the GoogleAccountCredentialpackaged in google-api-client-android-1.19.0.jarthat comes with Google APIs Client Library for Java. The package offers Utilities based on Google Play services client librariesand GoogleAccountCredential is just a wrapperaround GoogleAuthUtiland AccountPicker. This would allow you to use the same consistent authorization flow and the standard account picker dialog that comes Google Play services client library while delegating the token management to GoogleAccountCredential. Refer thisfor an example.

  • 直接使用来自 Google APIs Client Library for Java 的 OAuth 2.0 库(不是 android 的首选)

  • 或使用GoogleAuthUtilAccountPicker来利用 Google Play 服务库的授权部分。阅读 REST API 的 Google 授权。GoogleAuthUtil.getToken()缓存和管理令牌到期并自行刷新。但是,在网络错误/服务器负载的情况下,您可能需要在重试令牌之前使用指数退避算法,以免请求淹没服务器。)

  • 或使用Google APIs Client Library for Java附带的GoogleAccountCredential打包文件。根据包的报价工具谷歌Play服务客户端库和各地和。这将允许您使用相同的一致授权流程和来自 Google Play 服务客户端库的标准帐户选择器对话框,同时将令牌管理委托给. 请参阅示例。google-api-client-android-1.19.0.jarGoogleAccountCredential is just a wrapperGoogleAuthUtilAccountPickerGoogleAccountCredential

回答by GLee

Method number one (com.google.api.client.googleapis.extensions.android.gms.auth.*) is part of Google's more general, cross platform account management flow, while method number two (com.google.android.gms.auth.GoogleAuthUtil) is Android specific. It looks like the various Android Google APIs, method two, wrap and simplify method one. For instance, the Google+ documentation, second paragraph, states "The Google+ Sign-In button authenticates the user and manages the OAuth 2.0 flow, which simplifies your integration with the Google APIs."

方法一 (com.google.api.client.googleapis.extensions.android.gms.auth.*) 是 Google 更通用的跨平台帐户管理流程的一部分,而方法二 (com.google.android.gms. auth.GoogleAuthUtil) 是 Android 特定的。看起来像各种Android Google API,方法二,包装和简化方法一。例如,Google+ 文档的第二段指出“Google+ 登录按钮对用户进行身份验证并管理 OAuth 2.0 流程,这简化了您与 Google API 的集成。”

I would the Android specific method, token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope),wherever possible.

我会token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope),尽可能使用 Android 特定的方法。