访问 Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken() [英] Access to Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()

查看:24
本文介绍了访问 Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我看到了一些 Google 提供的示例,他们使用此语句来获取 GoogleAccountCredential 对象

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

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

但是,如果我查看文档,例如:
http://developer.android.com/google/auth/http-auth.html
http://developer.android.com/google/play-services/auth.html

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

他们都提到了以下用于获取令牌的方法:
token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope);

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

我很困惑在哪种情况下使用哪个以及为什么.我一直在使用方法号.1 成功,无需在首选项中保留令牌(我猜这是由 GoogleAccountCredential 自动完成的)

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. 谁能告诉我为什么有人会使用第一种方法而不是第二种方法?

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

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

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

推荐答案

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

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.

通过查看维基页面在项目中很难理解 Google API 客户端库与 GoogleAuthUtil 的关系,因为 wiki 建议 AccountManager 用于处理 Google 帐户,它根本没有提及 GoogleAuthUtil.

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.

但是,如果您深入研究代码及其问题跟踪器,您会发现 tasks sample 你实际上链接了使用 GoogleAuthUtil 自 已添加.

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.

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

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.

有关 AccountManager 和 GoogleAuthUtil 之间差异的更多信息,请参阅:简而言之与使用 OAuth2 请求 getAuthToken 和 getToken 的区别是什么

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

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

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

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

使用 Google API 客户端库的原因

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

使用 GoogleAuthUtil 的原因

  • 使用它不需要 Google Play 服务以外的其他库或外部依赖项
  • 您的应用的占用空间应该更小,因为您不必包含额外的库.
  • 如果您与 Google 的互动有限,那么直接使用 GoogleAuthUtil 而不是通过其他库可能会更容易.
  • GoogleAuthUtil 不应该像现在这样难以使用,因此使用围绕它的库来简化它可能不会那么容易使用.

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

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

如果您使用的是 Google API 客户端库并且它对您来说运行良好,我认为您没有理由不继续使用它.

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 服务交互的 Android(仅限)应用程序,我可能会直接使用 GoogleAuthUtil.

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

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

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

是的,这是由 GoogleAuthUtil 自动处理的,而后者又由 GoogleAccountCredential 使用.

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 ?

您应该能够调用方法 getToken().

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

这篇关于访问 Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆