Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed youtube videos using oembed

I want to use oembed to get the embed code from youtube links with jQuery:

var url = "http://www.youtube.com/watch?v=iwGFalTRHDA";
url = encodeURIComponent(url);

$.getJSON('http://youtube.com/oembed?url='+url+'&format=json', function(data) {
console.log(data);
});

Well I don't get any data.

Funny thing is, that if I browse to the url I get the right response:

http://www.youtube.com/oembed?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DiwGFalTRHDA&format=json` 

leads me to

{
provider_url: "http://www.youtube.com/"
title: "Trololo"
html: "<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/iwGFalTRHDA?version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/iwGFalTRHDA?version=3" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed></object>"
author_name: "KamoKatt"
height: 344
thumbnail_width: 480
width: 425
version: "1.0"
author_url: "http://www.youtube.com/user/KamoKatt"
provider_name: "YouTube"
thumbnail_url: "http://i2.ytimg.com/vi/iwGFalTRHDA/hqdefault.jpg"
type: "video"
thumbnail_height: 360
}

I also used the jquery oembed plugin, but the onError option is always thrown, also if the request was successful.

I'm really looking forward for some ideas...

like image 931
Karsten Avatar asked Aug 19 '11 15:08

Karsten


People also ask

What is YouTube oEmbed API?

Youtube supports oEmbed format. This means we can easily fetch information about a video, such as its thumbnail image, HTML for embedding, title etc. without having to use Youtube API or scraping the Youtube page. You can reach oEmbed API at: https://www.youtube.com/oembed?

How do you use oEmbed?

Here's an example of oEmbed: drop a YouTube video URL on its own line in (say) a blog post, and it will get replaced with an actual embedded video. That's just how CodePen oEmbed works, drop a Pen URL into the content area of an oEmbed-enabled site and it will be converted into an embedded Pen, automatically.

What is oEmbed URL?

oEmbed is a format that allows you to describe URL representations that can be embedded in other web pages. That is, it is a way by which a provider (YouTube, Twitter, Scribd, Flickr, etc) can describe to a consumer (our Drupal website, for example) how to display a provider's resource.


1 Answers

Actually the problem is you're violating the browser same origin policy with a cross domain ajax request. There a few work potential work arounds -- unfortunately the best JSONP, isn't implemented by YouTube. The next best is using Flash for transport. This is used by YUI-IO utility. Also you can see Jquery suggestions here.

like image 136
mjhm Avatar answered Sep 20 '22 17:09

mjhm