与OAuth2.0在Android YouTube数据API [英] Youtube Data API with OAuth2.0 on Android

查看:764
本文介绍了与OAuth2.0在Android YouTube数据API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用YouTubeData API和Android上的OAuth 2.0认证,而且我有点吃力的感觉。

I'm trying to use the YouTubeData API with OAuth 2.0 authentication on Android, and i'm kind of struggling with it.

我搜索了很多网上的,但有没有针对Android实现多大的帮助。

I've searched a lot online, but there's not much help for the Android implementation.

首先,这不是我清楚什么是获得的OAuth令牌的最佳方式。在doc他们建议,对于Android是更好地利用谷歌Play业务lib中获得。真的吗?如果是的话,它应该是pretty的琐碎按照本指南: https://开头的开发。 google.com/android/guides/http-auth 。 但在这一点上,我将有一个字符串对象令牌。我应该如何使用它与YouTubeData API?我应该在某处将其放置在 YouTube.Builder

First of all, it's not clear to me what's the best way to obtain an OAuth token. In the doc they suggest that for Android is better to obtain it using the Google Play services lib. Is that true? if yes, it should be pretty trivial following this guide: https://developers.google.com/android/guides/http-auth. But at this point i will have the token in a String object .. how should I use it with the YouTubeData API? Should I place it somewhere in the YouTube.Builder ?

YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
        public void initialize(HttpRequest request) throws IOException {
        }
    }).setApplicationName("AppName").build();

如果是的,没有人知道在哪里?

if yes, does anyone know where?

在搜索上的计算器我遇到了这个问题:<一href="http://stackoverflow.com/questions/17604593/what-to-do-after-getting-auth-token-android-youtube-api">What安卓的Youtube API - 获得身份验证令牌后做。在这里,易卜拉欣Ulukaya说,这是更好地使用 GoogleAccountCredential 。对于我所理解的(<一href="http://stackoverflow.com/questions/22142641/access-to-google-api-googleaccountcredential-usingoauth2-vs-googleauthutil-get">Access以谷歌API - GoogleAccountCredential.usingOAuth2 VS GoogleAuthUtil.getToken()) GoogleAccountCredential 的Andr​​oid版本应该使用 GoogleAuthUtil 从谷歌Play业务的lib提供的,因此它可能是pretty的有用简化这个过程。我看着从易卜拉欣Ulukaya( https://github.com/建议示例项目YouTube的/ YT-直接精简版,Android的),我已经实现了一切像他那样。但它似乎并没有很好地工作的我只获得该消息在logcat中:有一个IO错误:com.google.android.gms.auth.UserRecoverableAuthException:NeedPermission:空。
(请注意,我已经启用所有所需的API在谷歌控制台,并创建了客户端ID为我的应用程序)

Searching on StackOverflow i've come across this question: What to do after getting Auth Token - Android Youtube API. Here Ibrahim Ulukaya says it's better to use GoogleAccountCredential. For what i've understood (Access to Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()) the Android version of GoogleAccountCredential should use the GoogleAuthUtil provided from the Google Play services lib, so it could be pretty useful to simplify the process. I've looked at the sample project suggested from Ibrahim Ulukaya (https://github.com/youtube/yt-direct-lite-android) and i've implemented everything as he does. But it doesn't seem to work very well as i'm only obtaining this message in the logcat: "There was an IO error: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission : null".
(Note that I've enabled all the required APIs on the Google Console, and created the Client ID for my app)

在这一点上我迷路了。
如果我直接在 GoogleAuthUtil 使用来自谷歌Play业务的lib?在这种情况下,一旦得到令牌作为字符串如何使用它与YouTubeData的API?
或者我应该用 GoogleAccountCredential ?在这种情况下,有人知道我怎么能解决NeedPersmission:空?错误

At this point i'm kind of lost.
Should I use directly the GoogleAuthUtil from the Google Play services lib? in this case once obtained the token as a String how can i use it with the YouTubeData APIs?
Or should I use the GoogleAccountCredential ? in this case someone knows how can I solve the "NeedPersmission : null" error?

----编辑:

什么我的应用程序正在试图做的详细信息:作为这个我用这种原料药,我从简单的东西开始初体验:获取视频信息,然后播放这些视频,无需任何用户身份验证。我设法轻松地做到这一点pretty的,但我的应用程序的目的,我需要访问用户数据,特别是用户必须能够喜欢和评论的视频。 于是,我开始实施OAuth2,试图做同样的精确查询我在做之前(获取视频信息)。

details on what my app is trying to do: being this my first experience with this kind of APIs I started from the easy stuff: retrieve video information and then play those videos, without any user authentication. I managed to do that pretty easily, but for my app's purpose i need to access the user data, in particular users must be able to like and comment videos. So I started implementing OAuth2, trying to do the same exact queries I was doing before (retrieve video info).

推荐答案

哇。这个文档是超级混乱。充分披露,我不是一个Android开发者,但我是谁,已经与谷歌应用程序和OAuth2。

Wow. The documentation on this is super confusing. Full disclosure, I'm not an Android developer but I am a Java developer who has worked with Google apps and OAuth2.

谷歌播放或没有谷歌播放?首先,谷歌播放服务将只提供在Android设备上使用谷歌播放安装(所以不是欧雅,亚马逊装置等)服务。 谷歌的状态是的的谷歌播放库会给你最好的可能的业绩和经验。的。

Google Play or not Google Play? First off, Google Play Services will only be available on Android devices with Google Play Services installed (so not OUYA, Amazon devices, etc.). Google state that "the Google Play library will give you the best possible performance and experience.".

有很多的讨论(如<一个href="http://stackoverflow.com/questions/22142641/access-to-google-api-googleaccountcredential-usingoauth2-vs-googleauthutil-get">here, <一href="http://stackoverflow.com/questions/14365219/in-a-nutshell-whats-the-difference-from-using-oauth2-request-getauthtoken-and-g">here)从实际的Andr​​oid开发者,其中列出谷歌的种种好处播放经文的其他技术。我可以想象,一旦你能够得到你的应用程序使用一种方法的工作,那么它应该是一个很容易改变,如果你愿意的话。

There are numerous discussions (e.g. here, here) from actual Android developers that list the various merits of Google Play verses other techniques. I would imagine that once you are able to get your application working using one method, then it should be an easy enough to change if you so desire.

很多的例子code左右采用的是Android的AccountManager(任务和日历是最喜欢的例子),所以这就是我会展现出来。

Much of the example code about uses the Android AccountManager (Tasks and Calendars being favourite examples) so that is what I will show.

您例如code看起来似乎是一个简单的搜索,我猜想,许多在YouTube API交互并不需要OAuth2,在其他code我已经看到了这个空白的Htt prequestInitializer实施被称为无操作功能。 (例如,<一个href="https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/GeolocationSearch.java"相对=nofollow> GeolocationSearch.java )。

Your example code looks like it might be for a simple search, I would guess that many of the YouTube API interactions do not require OAuth2, in other code I've seen this empty HttpRequestInitializer implementation referred to as a no-op function. (e.g. GeolocationSearch.java).

这听起来像你想访问需要帐户凭据的YouTube API操作。你可以做类似这样的Andr​​oid日历例子东西(<一href="https://github.com/google/google-api-java-client-samples/blob/master/calendar-android-sample/src/main/java/com/google/api/services/samples/calendar/android/CalendarSampleActivity.java"相对=nofollow> CalendarSampleActivity.java ),除了与YouTube一样,从这个例子回答<一href="http://stackoverflow.com/questions/14489895/using-google-play-services-with-youtube">here.

It sounds like you want access to YouTube API operations that need account credentials. You can do something similar to this Android Calendar example (CalendarSampleActivity.java) except with YouTube, like the example answer from here.

// Google Accounts
credential = GoogleAccountCredential.usingOAuth2(this, YouTubeScopes.YOUTUBE, YouTubeScopes.YOUTUBE_READONLY);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
// YouTube client
service =
        new com.google.api.services.youtube.YouTube.Builder(transport, jsonFactory, credential)
            .setApplicationName("Google-YouTubeAndroidSample/1.0").build();

我希望这有助于。

I hope this helps.

这篇关于与OAuth2.0在Android YouTube数据API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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