YouTube and Vimeo provide a data API that you can use to get information for their videos. YouTube requires the use of an API key and you can get one for free. At the bottom of the article you will see the required steps to obtain a YouTube API key.
Vimeo on the other hand provides a simple API that does not require an API key. For authenticated read/write requests Vimeo provides the advanced API, which requires an API key. In this article we will see an example with the simple API.
In both cases we perform a web request and get back an JSON file with the video’s data. We will use the using System.Web.Helpers; extension to be able to read the data without writing a class to represent the JSON object.
Note: The post has been updated to reflect the recent changes of the YouTube API
using System.Net; using System.Web.Helpers; using System.Web.Script.Serialization; class Video { public const string ytKey = ""; public int Width { get; set; } public int Height { get; set; } public int Duration { get; set; } public string Title { get; set; } public string ThumbUrl { get; set; } public string BigThumbUrl { get; set; } public string Description { get; set; } public string VideoDuration { get; set; } public string Url { get; set; } public DateTime UploadDate { get; set; } public Video() { } public bool YouTubeImport(string VideoID) { try { WebClient myDownloader = new WebClient(); myDownloader.Encoding = System.Text.Encoding.UTF8; string jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=snippet"); JavaScriptSerializer jss = new JavaScriptSerializer(); var dynamicObject = Json.Decode(jsonResponse); var item = dynamicObject.items[0].snippet; Title = item.title; ThumbUrl = item.thumbnails.@default.url; BigThumbUrl = item.thumbnails.high.url; Description = item.description; UploadDate = Convert.ToDateTime(item.publishedAt); jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=contentDetails"); dynamicObject = Json.Decode(jsonResponse); string tmp = dynamicObject.items[0].contentDetails.duration; Duration = Convert.ToInt32(System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds); Url = "http://www.youtube.com/watch?v=" + VideoID; return true; } catch (Exception ex) { return false; } } public bool VimeoImport(string VideoID) { try { WebClient myDownloader = new WebClient(); myDownloader.Encoding = System.Text.Encoding.UTF8; string jsonResponse = myDownloader.DownloadString("http://vimeo.com/api/v2/video/" + VideoID + ".json"); JavaScriptSerializer jss = new JavaScriptSerializer(); var dynamicObject = Json.Decode(jsonResponse); var item = dynamicObject[0]; Title = item.title; Description = item.description; Url = item.url; ThumbUrl = item.thumbnail_small; BigThumbUrl = item.thumbnail_large; UploadDate = Convert.ToDateTime(item.upload_date); Width = Convert.ToInt32(item.width); Height = Convert.ToInt32(item.height); Duration = Convert.ToInt32(item.duration); return true; } catch (Exception ex) { return false; } } }
Note: The Vimeo API can give its data in XML, JSON and PHP format. This can be done easily by setting the URL’s suffix to ‘.xml’ or ‘.json’ or ‘.php’.
How to get a YouTube API key
- First, you need to login to https://console.developers.google.com/.
- Go to APIs and auth and then APIs. You need to search and enable the YouTube Data API as well as the Freebase API.
- Set its status to ON and accept it’s Terms of Use.
- Go to APIs and auth and then Credentials and create a new key in the Public API access area.
Note that this API key is not only for YouTube, but for many Google services such as Google Maps API, Custom Search API, etc.
I know this old post, but I am finding the update for it cause
“http://gdata.youtube.com/feeds/api/videos/” + VideoID + “?key=” + ytKey);
return “No longer available” where The YouTube Data API (v2) has been officially deprecated as of March 4, 2014.
for API V3 the following can be used instead
“https://www.googleapis.com/youtube/v3/videos?id=” + VideoID + “&key=” + ytKey + “&part=snippet”
and
“https://www.googleapis.com/youtube/v3/videos?id=” + VideoID + “&key=” + ytKey + “&part=contentDetails”
where the code should be changed also!
Hello Abdulla,
Many thanks on the notice! I will change my code and keep it up to date.
The post has been updated to reflect the recent changes of the YouTube API
Returns false when it gets to var dynamicObject = Json.Decode(jsonResponse);
Hey James, thanks for the feedback.
Seems that Vimeo has removed its Simple API and that is way is no longer working.
I will update the post (hopefully soon) to work with the API that currently provides.
I Ment i get an error at Json.Decode(JsonResponse) on the youtube as well.
I tested it and didn’t have any errors. Make sure that the VideoID you are using is from a public video. If you keep having the issue, consider making a post in Stack Overflow and post the link here.
here is a paste bin, haven’t gotten my stackoverflow request approved yet.
Code: http://pastebin.com/2Y39QWTa
Also i have the error here:
System.FieldAccessException: Attempt by method
‘System.Web.Helpers.Json.Decode(System.String)’ to access field
‘system.Web.Helpers.Json._serializer’ failed.
at System.Web.Helpers.Json.Decode(String value)
at TwitchBot_2._0.Form1.YouTubeImport(String VideoID)
I merged your last comments in one, when your SO account is ready, post the link of the question here.
The request was denied.
Nvm, i just had to disable the host process in the properties
should i keep the API secret? if yes how to? the youtube is warning me the the API public key is not restricted
Yes, you should keep it secret. If you have it public, others can use it and consume your quota.
Hello,
How i can download vimeo video by its url in android application programetically …?
Do you have any example code or specific class/Api or any other solution for it…?
Hi,
var dynamicObject = Json.Decode(jsonResponse);
“Json” Get Error
Please help me .
How To Resolve?
Thanks