How to use YouTube API for an iOS app? - ios

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

Related

Storing YouTube metadata

I've read YouTube's Terms of Service several times and have Googled this question tirelessly with no concrete answer. This is not a legal question about my specific YouTube API client, I just want to know if any YouTube developer has used the API in this way. I'm working on a side project for myself that's essentially an Instagram-YouTube mashup. I am a YouTube developer with an API key. Can my API client store a videos title, description and id in my database when my users like a video on my client? I don't know how else I can keep track of my users liked videos within my client.
Since you already specified that this is in fact not a legal question, the short answer is: Yes, you can (meaning it is possible).
There is one more possibility coming to mind, though:
You could authenticate users via OAuth 2.0 using the https://www.googleapis.com/auth/youtube scope. That way, you can manage their liked videos and add/remove videos to this list when a user clicks the according buttons on your website (you can also check if any given video is already liked by the user). In this case, all the track-keeping is done by YouTube itself. This ultimately means that whenever a user likes a video in your app, they also like it on the YouTube website. This may or may not be desired, it is for you to decide that.

Is it possible to host videos uploaded to my website on youtube?

I have Java based website. I would like users to log into my website and then upload videos to youtube using my youtube account. Users should not be required to have their own youtube account since videos will be uploaded using my youtube account.
Does youtube support this scenario?
If so, is there a sample code that shows me how to do this in Java?
I have used UploadVideo.java sample provided by google (https://developers.google.com/youtube/v3/code_samples/java#upload_a_video), but it requires users to log into youtube account using their id and password. That is not my use case.
Please study the terms and conditions of the Youtube service ; I think they do not allow this:
https://www.youtube.com/static?gl=GB&template=terms
YouTube accounts
4.1 In order to access some features of the Website or other elements of the Service, you will have to create a YouTube account. When
creating your account, you must provide accurate and complete
information. It is important that you must keep your YouTube account
password secure and confidential.
4.2 You must notify YouTube immediately of any breach of security or unauthorised use of your YouTube account that you become aware of.
4.3 You agree that you will be solely responsible (to YouTube, and to others) for all activity that occurs under your YouTube account.
and
5.1.L: you agree not to access Content or any reason other than your personal, non-commercial use solely as intended through and permitted
by the normal functionality of the Service, and solely for Streaming.
"Streaming" means a contemporaneous digital transmission of the
material by YouTube via the Internet to a user operated Internet
enabled device in such a manner that the data is intended for
real-time viewing and not intended to be downloaded (either
permanently or temporarily), copied, stored, or redistributed by the
user.
You can not let other people use your YouTube account/channel. The way to do is using YouTube Direct Lite
You basically add a ytdl with playlist tag while uploading videos.
You can check Android Client for how to do it in Java.
As to whether it is technically possible, yes, see https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
The technical problem here is users will be able to delete videos too if they have your access token with full permissions. basically you need to:
load your html page with the upload interface.
add to the onclick event of the upload button to send an ajax request to a script on your server which will:
return the access token of your account to the client
soon thereafter change the access token using the refresh token
Its still technically vulnerable though. a possible solution is to obtain the access token ONLY with the scope:
https://www.googleapis.com/auth/youtube.upload
There is a very good chance it will not be vulnerable then. You should test it.

Put vimeo videos in my website homepage

i need to put videos taken from a specific vimeo album. The videos are private (hide from video) so i need to use the advanced api but it seems that OAuth needs an user click to proceed. How can i show directly the videos?
First, if you are starting now I would recommend the new API
Second, if you are only ever using a single user you can just hard code your authentication into your app. Vimeo's documentation describes this feature in more detail.

Using YouTube API v3 to tell if a channel has a live stream

The goal of my YouTube API call is, given a channelId, to return whether that channel is currently live streaming. This is the call I'm making currently:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&eventType=live&type=video&key={YOUR_API_KEY}
While this call is functional, there is a significant delay between the channel starting a live stream and this call returning the stream.
Is there a better call to use in the YouTube v3 API that doesn't require oAuth? The functionality of my app is read-only.
Thanks!
Probably late but still someone else would use it, i found the answer on google api docs:
https://developers.google.com/youtube/v3/live/docs/liveBroadcasts/list
(Scroll to bottom, you can use their onsite api to make calls on the fly)
The call you have to make is:
GET https://www.googleapis.com/youtube/v3/liveBroadcasts?part=id%2Csnippet%2Cstatus&mine=true&broadcastStatus=active&key={YOUR_API_KEY}
(atm, they have an issue wth the status field). You can remove the filter and check the returned results for
{ "status": { "lifeCycleStatus": "live"}}
And as per google docs:
Before you start
You need a Google Account to access the Google Developers Console, request an >API key, and register your application.
Register your application with Google so that it can submit API requests.
After registering your application, select the YouTube Data API as one of the >services that your application uses:
Go to the Developers Console and select the project that you just registered.
Open the API Library in the Google Developers Console. If prompted, select a >project or create a new one. In the list of APIs, make sure the status is ON for >the YouTube Data API v3 and, if you are a YouTube Content Partner, the YouTube >Content ID API.
Calling the Data API
The API request must be authorized by the Google Account that owns the >broadcasting YouTube channel.
You can check this link for generating an access(OAuth 2.0) token: https://developers.google.com/identity/protocols/OAuth2?hl=en
I hope this helps.
I was digging for a "cheaper" way to find if a channel is live to save some API quota. I attempted to use Konstantin's workaround by looking at the {channel/channel_id}/live but this appears to not work anymore.
The channel no longer redirects when a person is live. Instead it runs on that page.
If they have a username URL then /c/ works: https://www.youtube.com/c/USER_NAME/live
If they have don't have a username and use the default like UC4R8DWoMoI7CAwX8_LjQHig, then you need to use https://www.youtube.com/channel/USER_NAME/live
The /search call is rather expensive. If you are only allotted the initial 10k quota points, you'd run out of points after only 100 queries. That may not be a bother for some use cases, but it is nevertheless limited.
Instead, you can use Playwright and do the following:
page.goto("https://YouTube.com/channel/{channel id}/live")
Then check for a redirection which will happen when the channel is live:
const redirect = page.url()
If redirect contains a link to a YouTube video, then you know the channel is live. Otherwise it is not live and will yield a link similar to the one that's passed in to the goto() function.

How to allow users to upload video from my website to my youtube account?

Is it possible to allow uses to upload video to my youtube account with v3 api? I want allow users to upload video through my website to youtube but not ask them to login to their youtube account. I know it was possible in v2 api. Is it also doable in v3 api?
I've tried so hard to do the same thing as you /w the v3 API. The principal difficulty is to set our credentials in the code. With the v3 API, all the old login methods are "deprecated", to lead place to oAuth V2 login method.
In the API, i've didn't found a parameter where to set credentials. I hope you can find it. Let me know if this is the case.
If you success to create an httpClient object /w your credentials, this is perfect, the code is easy after doing this.
In the case you seach the right code for the v2 version : here
Good luck :)
I'm digging into this question now, too. So far, one workaround is to use the email upload option (found within Youtube's settings once logged in).
There's a limit of 25mb here so it's probably just a temporary proof of concept but powerful enough for smaller vids (mixed with FFMpeg tools wrapped for the language of your choice, I'm using Node.js http://www.benfarrell.com/2012/06/14/morph-your-media-with-node-js/)
Ideally, I want a user to upload a vid, store it in RAM, and then get it pushed to my youtube channel. We'll see what we find...

Resources