How to use YouTube API for an iOS app?

Balázs Vincze picture Balázs Vincze · May 1, 2015 · Viewed 10.5k times · Source

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!

Answer

user4657588 picture user4657588 · May 1, 2015

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