Yesterday one of my clients (roundpeg) asked for a ColdFusion interface to the YouTube API. This turned out to be rather simple since they made use of a REST API. From this work I was able to build a CFC to work with all the functions defined in the API. This lets you get video information, profile information, search for videos etc. Best of all - roundpeg, Inc was cool with me releasing the code. You can download the CFC below.
Now the bad news is that YouTube will eventually be switching to a GData based API. I am not a fan of Google's APIs so frankly I can't see this as being a good thing - but I'll worry about that when the API is updated.
Here are some sample calls:
2
3 <cfset yt = createObject("component", "youtube").init(devid)>
4
5 <!--- get videos by user --->
6 <cfdump var="#yt.getVideosByUser('joerinehart')#" label="Videos by user.">
7
8 <!--- get music videos tagged Lush --->
9 <cfdump var="#yt.getVideosByCategoryAndTag(10,'Lush')#" label="Lush music videos." top="10">
p.s. Note that you need to get a developer profile and key before you use the code.
Comment 1 written by Tampa ColdFusion on 21 August 2007, at 10:51 PM
Thanks for saving me the time!
Comment 2 written by CeeVee on 22 August 2007, at 9:57 AM
Comment 3 written by Raymond Camden on 22 August 2007, at 9:59 AM
Comment 4 written by CeeVee on 22 August 2007, at 11:02 AM
Another simple question... using the getVideosByTag function, how would I get the total # of records returned so I could set up "paging" links? It seems that I need to have a default perpage value. Without the total # of records returned based on my search term, I cannot create the paging links.
Comment 5 written by Raymond Camden on 22 August 2007, at 12:09 PM
FYI, I have a very cool demo I'll be blogging soon. Mixes CF8+YouTube.cfc together.
Comment 6 written by CeeVee on 23 August 2007, at 7:57 AM
"Each youtube.videos.list_by_tag response includes the
video_list.total element, which contains the total
number of records."
I can't figure out what I need to do using your CFC to view the total number of records. Am I missing something? Could you help?
Comment 7 written by Raymond Camden on 23 August 2007, at 8:00 AM
Comment 8 written by CeeVee on 23 August 2007, at 8:10 AM
BTW, either go buy Bioshock or add to your wishlist. It's amazing.
Comment 9 written by Raymond Camden on 23 August 2007, at 8:15 AM
Ok the code is done. I'll be uploading to RIAForge in 5 minutes.
Comment 10 written by FALCONSEYE on 16 October 2008, at 1:46 PM
I am trying to use your cfc to upload videos to youtube but got some errors.
The cfhttp statement on youtube.cfc line 484 is not returning anything. When I do a cfdump, I see an empty authtoken. Could you please help?
Comment 11 written by Raymond Camden on 16 October 2008, at 1:50 PM
Comment 12 written by FALCONSEYE on 16 October 2008, at 2:41 PM
<cfset devid = "AI39si4J8K77YGl7yRXabWq9CFb0M-86TcuYFcYlEmnMDv4XL3OtoK7q1N4ApXYC5Y5ntbFurXGCmGi_U8mRy-CdUHezoMLIXQ">
<cfset yt = createObject("component", "youtube")>
<cfset yt.setDeveloperKey("#devid#")>
<cfset yt.login("myUserName", "myPassword")>
<cfset r = yt.upload('video.wmv','Test Upload','Playing with youtube','Music','testing,fun')>
This returns no error. When I do the following:
<cfdump var="#yt.getVideosByUser('myUserName')#" label="Videos by user.">
The cfdump returns nothing. Do you think this is still caused by the same problem?
Comment 13 written by Raymond Camden on 16 October 2008, at 2:43 PM
Comment 14 written by Jason Brookins on 20 October 2008, at 11:31 AM
Any word on the hotfix for CF7/CF8 from Adobe?
Also, what is the best way to go about tweaking the YouTube CFC for CF7 usage?
Comment 15 written by Jason Brookins on 20 October 2008, at 11:35 AM
"The APIs accessed by this developer ID program have been deprecated and are no longer supported. To use the new APIs or get information about migrating your applications, please visit the YouTube Developer site at code.google.com."
Does that impact YouTube CFC?
Comment 16 written by Raymond Camden on 20 October 2008, at 11:51 AM
@Jason - 2: It should be CF7 compat. The hot fix they are releasing will be for CF7 as well. If I did use any CF8 functionality, it was probably by accident and a small snippet like X++, which would need to change to x=x+1.
@Jason - 3: This was all tested with the new API.
Comment 17 written by Sasha on 11 November 2008, at 2:50 PM
Comment 18 written by Raymond Camden on 11 November 2008, at 3:12 PM
Comment 19 written by Anthony Escribens on 5 October 2009, at 8:58 PM
First of, great job on youtube.cfc !
Do you have an example on how to delete a video? I saw the function on the .cfc page and tried calling it using the following:
<cfset yt = createObject("component", "/yt/youtube")>
<cfset yt.setDeveloperKey("mydevkey")>
<cfset yt.login("myusername", "mypassword")>
<cfset r = yt.delete('#form.deleteytvideoid#')>
But it doesn't seem to be working. I'm only passing the video id as "form.deleteytvideoid". I am missing anything?
Thanks!
Anthony
Comment 20 written by Anthony Escribens on 5 October 2009, at 9:33 PM
I found the answer to my question above.
The url had actually changed to:
<cfset var theurl = "http://gdata.youtube.com/feeds/api/users/#variable...;
Now it's working!
Thanks again for a great code and everything you do for the CF Community!
- Anthony
Comment 21 written by Raymond Camden on 5 October 2009, at 11:08 PM
Comment 22 written by Anthony Escribens on 7 October 2009, at 7:15 AM
When uploading a video, what does it mean when I get this error:
"YouTubeCFC Upload Error: Domain=yt:validation, Code=too_short"
Does it have to do with the number of entries into the video name or description?
Thanks,
Anthony
Comment 23 written by Raymond Camden on 7 October 2009, at 10:33 AM
Comment 24 written by Anthony Escribens on 7 October 2009, at 6:00 PM
<cfset yt = createObject("component", "/yt/youtube")>
<cfset yt.setDeveloperKey("<key>")>
<cfset yt.login("<login>", "<password>")>
<cfset r = yt.upload('<path_to_video>','Cow Test Number 3','This is a test description for the new cause/project.','Music','Cow Test Number 3, United States, Charity, Donation, Help, ')>
- Anthony
Comment 25 written by Raymond Camden on 8 October 2009, at 12:26 AM
Comment 26 written by Anthony Escribens on 9 October 2009, at 5:03 PM
Comment 27 written by santosh on 15 December 2009, at 12:09 AM
this is my code:
<cfset yt = createObject("component", "youtube")>
<cfset result = yt.getVideosByTag('#form.tag#')>
<!---<cfdump var="#result#">--->
<table width="100%" height="100%" border="0">
<cfoutput>
<cfset catname = "#form.cat#">
<h4>Reslts for Tag : <em>#form.tag#</em> and Category : <em>#catname#</em></h4>
<cfloop query="result" >
<cfif catname eq "#result.CATEGORIES#">
<cfset vid=listgetAt('#result.THUMBNAIL_URl#',4, '/')>
<tr><td>Title : #result.TITLE#</td></tr>
<tr><td><a href="test_getvideo.cfm?v=#vid#" ><img src="#result.THUMBNAIL_URL#" /></a></td></tr>
</cfif>
</cfloop>
</cfoutput>
</table>
the function i used is:
<!---Changed to GDATA yotube API--->
<cffunction name="getVideosByTag" access="public" returnType="query" output="false" hint="Searches videos by tag.">
<cfargument name="tag" type="string" required="true">
<cfargument name="page" type="numeric" required="false" default="1">
<cfargument name="perpage" type="numeric" required="false" default="40">
<cfset arguments.perpage = pageFix(arguments.perpage)>
<cfreturn getVideos("#variables.rooturl#feeds/api/videos?vq=#urlEncodedFormat(arguments.tag)#&page=#arguments.page#&per_page=#arguments.perpage#")>
</cffunction>
Comment 28 written by Raymond Camden on 16 December 2009, at 8:37 PM
[Add Comment] [Subscribe to Comments]