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

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

问题描述

最近,我一直在努力了很多与谷歌Android的API尤其是Analytics(分析),AdSense和任务的API。

我已经看到由谷歌提供了一些样品,他们用这个语句来获得 GoogleAccountCredential 对象

<一个href="https://$c$c.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://$c$c.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(这一点,Collections.singleton(TasksScopes.TASKS));

不过,如果我办理的文件,如:
<一href="http://developer.android.com/google/auth/http-auth.html">http://developer.android.com/google/auth/http-auth.html
<一href="http://developer.android.com/google/play-services/auth.html">http://developer.android.com/google/play-services/auth.html

两者都提到以下方法被用于获得令牌:
标记= GoogleAuthUtil.getToken(mActivity,mEmail,mScope);

我很困惑这场景,为什么要使用哪一个。使用方法没有我一直在。 1成功,没有坚持的令牌preferences的需要(我猜这是由GoogleAccountCredential自动完成)

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

  2. 我如何可以访问身份验证令牌中的第一个方法是什么?

解决方案

谷歌的API客户端库对Java 是顾名思义的库来访问谷歌的API,它可用于多种平台,如Java(一般)和Android,而的谷歌播放服务和<一href="http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html">GoogleAuthUtil仅适用于Android系统。

通过观察的的维基页面项目是很难理解谷歌的API客户端库如何与GoogleAuthUtil因为维基表明,<一个href="http://developer.android.com/reference/android/accounts/AccountManager.html">AccountManager用于处理谷歌账户,它并没有真正提到GoogleAuthUtil的。

然而,如果你深入到code和问题跟踪了一下,你可以看到,<一个href="https://$c$c.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">tasks样品你实际上链接<一个href="https://$c$c.google.com/p/google-api-java-client/source/browse/google-api-client-android/src/main/java/com/google/api/client/googleapis/extensions/android/gms/auth/GoogleAccountCredential.java#255">uses GoogleAuthUtil因为<一href="https://$c$c.google.com/p/google-api-java-client/wiki/ReleaseNotes#Version_1.12.0-beta">version 1.12.0 在谷歌的API客户端库时,对GoogleAuthUtil支持是的补充

维客大概提AccountManager的,而不是GoogleAuthUtil因为这是做OAuth2认证前GoogleAuthUtil可用的方式,因为维基的那一部分还没有被更新。

有关的AccountManager和GoogleAuthUtil之间差异的详细信息,请参见:<一href="http://stackoverflow.com/questions/14365219/in-a-nutshell-whats-the-difference-from-using-oauth2-request-getauthtoken-and-g/14374577">In概括地说有什么区别使用OAuth2要求getAuthToken并为gettoken

总之谷歌的API客户端库是一个跨平台库,与谷歌的服务和Android版本使用GoogleAuthUtil实现交互。

  

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

理由使用谷歌的API客户端库

  • 如果您正在开发一些其他的平台比Android则无法使用GoogleAuthUtil,因为它是一个Android特定库。
  • 如果你正在开发一个跨平台的应用程序,您可以使用谷歌的API客户端库的共享code为Android和其他平台。
  • 如果您有很多相互作用的许多谷歌的服务,这个库可以让事情变得更容易。
  • 如果您已经在使用这个和它的作品通缉确实没有任何缺点继续使用它,因为它是让你得到GoogleAuthUtil所有与使用基于的AccountManager或一些其他库的优势,一个包装GoogleAuthUtil上的AccountManager。

理由使用GoogleAuthUtil

  • 在使用此无需其他库或外部的依赖比谷歌播放服务
  • 您的应用程序的足迹应该是较小的,因为你没有包含额外的库。
  • 如果您与谷歌的互动仅限于只使用要去槽另一个库的GoogleAuthUtil直接相反,它可能会更容易。
  • GoogleAuthUtil不应该认为很难使用,因为它是,因此,使用该环绕它的库来简化它可能不是那么更容易使用。
  

我很困惑这场景,为什么要使用哪一个。使用方法没有我一直在。 1成功...

如果您使用的是谷歌的API客户端库,它工作正常的你,我看不出有任何理由你不应该继续使用它。

但是,如果我想创建一个需要与谷歌的服务进行交互我可能会使用一个机器人(只)应用GoogleAuthUtil直接。

  

...没有坚持preferences令牌的需要(我猜这是由GoogleAccountCredential自动完成)

是的,我这是自动GoogleAuthUtil后者又用GoogleAccountCredential处理。

  

我如何可以访问身份验证令牌中的第一个方法是什么?

您应该能够调用方法<一href="http://javadoc.google-api-java-client.google$c$c.com/hg/1.17.0-rc/com/google/api/client/googleapis/extensions/android/gms/auth/GoogleAccountCredential.html#getToken%28%29">getToken()在 GoogleAccountCredential 对象。

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 ?

解决方案

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.

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

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