如何将YouTube API用于iOS应用程序? [英] How to use YouTube API for an iOS app?

查看:140
本文介绍了如何将YouTube API用于iOS应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索了教程和说明,但是找不到任何关于此的信息......所以我希望也许有人能指点我正确的方向,或者只是链接一些好的教程。 (顺便说一句,我愿意制作一个基本的YouTube客户端应用程序,它可以让用户搜索视频,而不是看他们......没什么特别)。 欢迎来到解析JSON文件并使用BADLY记录的API的世界:)

那么我在几个月前正在研究这个问题,并且设法让它工作。以下是您必须执行的操作:

第1步 - OAuth 2.0集成

您的应用是否允许用户登录YouTube,以便他们可以评论/喜欢/ etc ...视频?如果是这样,那么您需要先让用户通过您的应用先登录到YouTube API,然后才能做这些事情。为此,您的应用需要使用OAuth 2.0与YouTube API进行通信。以下是一个极好的图书馆,您可以使用该图书馆通过OAuth 2.0对YouTube API进行身份验证: https://github.com / BHughes3388 / BAHYouTubeOAuth



第2步 - 钥匙串 - 存取令牌存储

如果您对OAuth身份验证不熟悉,那么开始时可能会令人望而生畏,但一旦您熟悉了OAuth身份验证并熟悉它,您就会意识到使用它非常容易。



整体而言,它的工作方式是,您的应用程序联系API(本例中为YouTube V3 API)并请求网页。该网页允许用户登录到APi并授予您的应用访问权限。完成此操作后,该API将向您的应用程序发送一个访问令牌。这个令牌将需要用于所有(或大部分)API请求中(这取决于请求的类型)。

一旦获得了访问令牌那么你需要安全地存储它。它基本上是一个密码,所以你需要保持安全。做不是做某些人做的事,即使用 NSUserDefaults ,这根本不安全。相反,你需要使用钥匙串。这是Apple的安全加密/存储库,可以轻松地用于保存安全的字符串,如密码。



为了使用Keychain安全地存储和检索访问令牌当你需要它时,看看这个SO帖子,它很棒:



第3步 - YouTube API V3 - https://developers.google.com/youtube/v3/



现在就是这样,难一点。您需要通读Google YouTube API V3并浏览API参考文档以找到将返回您正在查找的数据的特定URL。例如:如果您想从用户的主页提要或简单搜索中获取视频列表,请查看此API参考页面: https://developers.google.com/youtube/v3/docs/videos/list



第4步 - 提取视频网址



在我进一步讨论这一步之前,我应该提到Google说你应该通过简单的网络视图与YouTube HTML代码呈现视频,这些代码只是将视频视图嵌入到您的应用中。但在我看来(和许多其他开发者),这看起来和工作可怕....所以,如果你想通过本地MPMovieplayerController在您的应用程序中显示YouTube视频,那么您需要首先提取视频网址,然后才能执行此操作。



当您向YouTube API申请一组视频时,你会得到一个JSON文件,这个文件包含一个视频列表(videoIDs,titles,dates等)。

你需要解析视频ID的JSON文件。一旦您或应用用户选择了他们想要的视频,就需要使用该视频ID,然后将该视频的视频ID传递给YouTube视频链接提取器库。然后该库将返回该视频的一组视频文件链接。这些链接可以与MPMovieplayerController一起使用,以便本地显示视频。



以下是一个很棒的YouTube视频文件url extracter - https://github.com/runmad/RMYouTubeExtractor



祝你好运:)) p>

I have searched the web for tutorials and instructions but could not find any on this... So I was hoping maybe someone here can point me in the right direction or just link some good tutorials. (By the way I am willing to make a basic YouTube client app which lets the user search for videos and than watch them...nothing too fancy).

Thanks in advance!

解决方案

Oh buddy.... welcome welcome to the world of parsing JSON files and putting up with BADLY documented APIs :)

Well I was looking into this a few months ago and I managed to get it working. Here are the things you have to do:

Step 1 - OAuth 2.0 Integration

Will your app allow the user to login to YouTube so that they can comment/like/etc... videos? If so, then you will need to get the user to login to the YouTube API via your app first before they can do these kind of things. In order to do that, your app will need to use OAuth 2.0 to communicate with the YouTube API. Here is a superb library which you can use to authenticate with the YouTube API via OAuth 2.0: https://github.com/BHughes3388/BAHYouTubeOAuth

Step 2 - Keychain - Access Token storage

If you are new to OAuth authentication, then it can be daunting at first, but once you play around with it and familiarize yourself with it, you will realize that it is very easy to work with.

So overall, the way it works, is that your app contacts the API (in this case YouTube V3 API) and requests a webpage. The webpage allows the user to login to the APi and grant your app access. Once this has been done, the API will send your app an "access token". This token will need to be used in all (or most) of your API requests (it depends on the type of request).

Once you have obtained the "access token" you will then need to store it securely. Its basically a password, so you need to keep it safe. Do not do what some people do, which is to use NSUserDefaults, that is not secure at all. Instead you will need to use Keychain. This is Apple's secure encryption/storage library which can easily be used to save secure strings such as passwords.

In order to use Keychain to securely store and retrieve the "access token" when you need it, take a look at this SO post, its great: How to use Keychain for saving password like GenericKeychain sample code

Step 3 - YouTube API V3 - https://developers.google.com/youtube/v3/

Right so now, the harder bit. You need to read through the Google YouTube API V3 and go through the API reference documents to find the specific URLs which will return the data you are looking for. For example: if you want to get a list of videos from the home feed of a user or from a simple search, then take a look at this API reference page: https://developers.google.com/youtube/v3/docs/videos/list

Step 4 - Extract the video URL

Before I talk further about this step, I should mention that Google says you should present videos in a simple webview with YouTube HTML code which simply embeds the video view in your app. But in my opinion (and many other devs), this looks and works horribly....

So if you want to display YouTube video in your app via the native MPMovieplayerController, then you will need to extract the video URL first before you can do this.

When you make a request to the YouTube API for a set of video(s), you will get a JSON file back, this file contains a list of videos (videoIDs, titles, dates, etc....).

You will need to parse the JSON file for the video IDs. Once you or the app user has selected the video that they want, you will need to use then pass the video ID for that particular video to a "YouTube video link extractor" library. This library will then return a set of video file links for that video. These links can be used in conjunction with MPMovieplayerController to display the video natively.

Here is a great YouTube video file url extracter - https://github.com/runmad/RMYouTubeExtractor

Good luck :)

这篇关于如何将YouTube API用于iOS应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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