YouTube API, Version 3 on Rails

Originally published at: http://www.sitepoint.com/youtube-api-version-3-rails/

A while ago, I penned an article on using YouTube on Rails, explaining the basics of interacting with the YouTube API. The previous article covered how to fetch video information and use the YouTube IFrame API to manipulate video player. Later, another article, Uploading Videos to YouTube with Rails, was released showing how to create an app that allows users to upload videos directly to YouTube.

The Youtube_it gem was used for both demos. Both of my posts garnered quite an bit of comments, which I appreciate.

youtube_it is a great gem, however, it employs version 2 of the YouTube API, which is now officially deprecated and will no longer be supported after April 20th, 2015. Ouch.

Fortunately, Claudiofullscreen saved the day by creating a new gem called yt (now that’s a short name) that utilizes version 3 of the YouTube API.

In this article, we are going to build an app similar to the one that was introduced in the “YouTube on Rails” and “Uploading Videos to YouTube with Rails” posts, but make it work with version 3 of the YouTube API.

Continue reading this article on SitePoint

hello mr. bodrova

maybe you can help me…i got an message
uninitialized constant OpenSSL::SSL::SSLErrorWaitReadable when i try to upload a video…

Hello!

Its “Bodrov”, but call me Ilya plz :smile:

I and some other coders stumbled upon this issue and it is still unclear what is the root cause. See this: https://github.com/Fullscreen/yt/issues/103 and https://github.com/Fullscreen/yt/pull/110 The author of the gem said that he had when YT API was experiencing some problems and after a while it is gone. I’d recommend checking that all required APIs are enabled (check the second screenshot here https://github.com/Fullscreen/yt#configuring-your-app).

You might re-open the #103 issue and describe when the problem occurs. Hopefully this we be solved this way or another.

:smile: ok Ilya thank you and i will check that…

This is a great tutorial. Thanks very much for providing it. Do you know if the Youtube API quotas apply here? If quotas apply (I would think they would for some actions), do you know if the quota is applied per the site that is calling the api or per the user that is causing the api to be called–obviously, if the quotas apply per user, that would enable a lot more activity than if they apply every time the site calls the api. It is a little confusing how using the gem maps to the different types of APIs Youtube describes, which have their own quota limits.

I assume the quotas apply, but am not sure how. It looks like there are separate quotas for anyone using Youtube Data API and Youtube Analytics API (not sure about Youtube Player API, which might be most on point). There might not be a quota for simply embedding a video, but I am pretty sure there is a quota for uploading a video from a site to youtube. Seems like this could be called “insert”: https://developers.google.com/youtube/v3/getting-started#quota. If so, by the quota rules (getting pretty specific here), that costs about 1600 units, and you are alotted 50,000,000 units a day. That would translate to about 30,000 videos allowed to be uploaded per day. That is obviously a lot and not a concern for building something just for learning, but if your site starts getting a lot of traction, it could be a little bit of a concern if users could at most do 30,000 a day (3 mil a month).

Any way that quota might be taken per user? (so your site could upload any amount, but each user would be limited to 30,000 a day–obviously way more than enough)

And, as said, do you think the quotas apply for both just embedding an already existing quota and uploading a new video, or just uploading a new video?

There are a few unanswered Stack Overflow questions on this, so I think it is a point of confusion.

Thanks again!

That is a really good question, but unfortunately I can’t answer it 100%. Quota is really being applied for any API interaction, however it is not applied to Iframe API when you embed videos (because you are not providing any keys or tokens). If you open Google Developers Console and navigate to API section of any project, there will be Enabled APIs tab. Inside you will see quota per API. Therefore interacting with YT Analytics in not the same as interacting with YT Data API. These quotas are counted per project.

What I do not know is how to set up those per user and seems like from this console there is no way to do that (moreover it seems that there is no way to do it at all, see below). I believe you can search for such topics in Google groups or contact them directly. I have heard somewhere (though I am really not sure if it was related to Google APIs) that if you need much more API calls for a project, then you may contact Google team and discuss this personally. For example, here is some info on usage limits for Google maps https://developers.google.com/maps/documentation/javascript/usage It clearly states that if your app generates too many traffic, Google team will contact you to discuss payment options. Some more info on billing: https://developers.google.com/console/help/new/#billing Money makes the world go around, as they say.

Hopefully that helps and thank you for the feedback!

Thanks. Your reply adds clarity to how this might work with the Google quotas–and I doubt there is much more to know without talking to Google directly. I appreciate it.

Hey Ilya (@bodrovis)

This tutorial was my introduction to ruby and may I say that it was very well done, everything made perfect sense and was easy to implement and all is working perfectly.

However I need my system to do something a little differently, and I can’t find to many articles regarding this on rails.
I need my site to have one central youtube account, that all the videos are submitted too, regardless of the end-users on my site. I believe its like a content owner account but not 100%, maybe you know of a couple of links you can point me too.

I know their is a lot of content copy right issues involved, but we would own the rights to the videos submitted, so that is not an issue in this case.

Any assistance would be appreciated.

Thank you for the feedback!

Well, the only thing that I can suggest is service accounts https://developers.google.com/identity/protocols/OAuth2ServiceAccount - it might help in your case. I don’t know about any other possible solutions :frowning:

Hey Ilya (@bodrovis)

That is exactly what I’m busy with now, and once you have the access token from your service account, you can use the yt gem as per normal.

Thanks a lot for your quick feedback :slight_smile:

Good luck then!

@bodrovis - Hi Ilya,

Thanks so much for this tutorial, incredibly helpful for the project I am currently working on.

One minor point which my team got stuck on for a couple of hours together around the Google+ OAuth step.
We were trying to run in local host following your exact implementation instructions but continued to get redirect_url, CSRF detected and SSL errors throughout troubleshooting.

2 things worked for us that are worth flagging to other people following:

  1. Deployed to heroku - this may have been implicit in your instructions but this was only scenario that the specific route matching worked for us
  2. For local deployment, we had to change our callback url reference in our Google project credentials to localhost vs /127.0.0.1 AND in our omniauth initializer add the line OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE if Rails.env.development? to disable SSL certificate verification
  1. Yeah, that was implicit
  2. That’s interesting, as I’ve never met this problem with certificate verification.

Thank you for these tips, should be helpful for other readers! :smile:

@bodrovis - no problem

I have one follow-up question around validating video uploads.
With this structure, how would you implement file size validations for video uploads?
We only want to allow short videos e.g. < 60 secs in length and to begin with want to set an arbitrary file size limit of 50MB.

We have explored trying to add explicit validations within the video_upload.rb ActiveType::Object but to no avail.
e.g. validates_size_of :file, maximum: 50.megabytes, message: "should be less than 50MB"
I understand how you could achieve this using a third party uploader like CarrierWave but struggling to see how you could do this here.

Any thoughts would be greatly appreciated!

Thanks,
Alex

That’s an interesting question. I will try to look into it and let you know the results. Judging by this https://github.com/Fullscreen/yt/issues/20 yt currently loads the whole file into memory so we may work with it somehow.

Thank you @bodrovis for this tutorial. Has helped me and the project I am working on tremendously.

I am running into an issue with the YouTube authentication however. This is the error I am getting:

A request to YouTube API was sent without a valid authentication: {"error"=>{"errors"=>[{"domain"=>"global", "reason"=>"authError", "message"=>"Invalid Credentials", "locationType"=>"header", "location"=>"Authorization"}],

According to the application trace this is coming from the upload! method in video_upload.rb; the access token seems to be inauthentic. I’m not exactly sure if this is the case or how to go about fixing it because it does work in some instances (e.g when the db is empty).

Any advice would be greatly appreciated. Thank you!

Hi! As far as I see you’ve opened this issue here https://github.com/Fullscreen/yt/issues/230 so I’ll monitor it and discuss there.

Thanks! I’ve identified the problem and responded accordingly https://github.com/Fullscreen/yt/issues/230#issuecomment-130236596

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.