YouTube API: Dive-In HigEdWebDev 2009 Ron West
Dec 17, 2014
YouTube API: Dive-In
HigEdWebDev 2009Ron West
Google's mission
• …is to organize the world's information and make it universally accessible and useful. Sometimes making information accessible requires making it available in contexts other than a web browser.
Agenda
• Introduction• Getting Started• Get Content• Interaction• Pulling it Together• Developer Community
Introduction
YouTube in ReviewREST
Google Data APIYouTube Data API
Entries/FeedsAuthentication
YouTube in Review
• Videos• Comments• Ratings• Users• Categories• Keywords/Tags• Channels• Subscriptions
• Messages• Activity Feeds• Recommendations• Friends• Related Videos• Captions• Favorites• Insight Data
REST
• REpresentational State Transfer• Architectural Style – NOT – Standard• More akin to Doc Literal than RPC based Web
Service• Logical URL’s vs. Static URL’s
REST
• Client-Server• Stateless• Cacheable• Uniform Interface• Layered
REST
• HTTP Methods– Get = List or Retrieve– Put = Replace or Update– Post = Create– Delete = Delete
SOAP (RPC/Doc Literal) Web Services
Pros •Single endpoint for all calls• Lanugage, platform and transport agnostic• Built in error handling
Cons• Conceptually more difficult• Harder to develop with (requires tools)
Multiple Endpoints
Pros• Language and platform agnostic• Simpler to develop than SOAP• Concise
Cons• Lack of standards• Tied to HTTP transport
REST Web Services
• Get Video(s)• Edit Video• Delete Video
Get API URL’s for:• Comments• Related Videos• Ratings• Mobile• Versions
Google Data API
• RSS vs. Atom• APP• Get vs. Post vs. Put vs. Delete
Response Codes
• Same response codes for HTTP– 200, 201, 301, 400, 500 etc..
• Codes have similar meaning for each type of call– 200 (OK)– 400 (Bad Request)– 401 (Not Authorized)– 404 (Not Found)– 500 (Internal Error)
• Accompanied with error details (XML in payload)
YouTube Data API
• Version 1• Version 2 (recommended)
YouTube Data API
• Search for Videos– Keywords (tags)– Categories– Query Params (metadata)
• Search for Playlists– Keywords (tags)– Categories
• Search for Channels– Keywords (tags)– Categories
YouTube Data API• Retrieve
– Videos– Comments– Captions– Related Videos– Video Responses– Categories– Keywords– Playlists– Subscriptions– Profiles– Contacts– Messages– Activity Feeds– Insight Data Feed– Favorites
YouTube Data API (cont.)
• Create– Videos– Comments– Captions– Related Videos– Video Responses– Complaint– Rating– Contact– Message– Favorite– Playlist– Subscription
YouTube Data API (cont.)• Update
– Video Metadata– Captions– Profiles– Playlists– Contacts
• Delete– Video– Message– Favorite– Playlist– Subscription– Contact
YouTube Data API
• Batch Processing– Process multiple types of calls in a single request– Each Request in its own <entry> tag
Sample Interaction
• Query API for videos tagged with “SHU”• Display Videos in List• Allow user to Select Video to Display• Display Single Video• Retrieve Comments for that Video• Post Comment• Login• Add to Playlist
Feed<feed [namespaces]>
… metadata …<openSearch:totalResults /><openSearch:startIndex /><openSearch: itemsPerPage /><entry>
… metadata …<media:group/>
</entry> <entry>
… metadata …<media:group/>
</entry>…
</feed>
Entry (video)<entry [namespaces]>
<title/><content/>… metadata …<link rel=“http://gdata.youtube.com/schemas/2007#video.responses “/><link rel=“http://gdata.youtube.com/schemas/2007#video.related” /><link rel=“http://gdata.youtube.com/schemas/2007#mobile”/><media:group>
<media:content yt:format=“t” url=“…”/><media:thumbnail url=“…” width=“…” height=“…”/><yt: duration /><yt:videoid />
</ media:group ></entry>
Authentication
• AuthSub – YouTube Authentication with “callback” API [difficult]
• OAuth – Standardized Approach (similar in nature to OpenID) [most difficult]
• ClientLogin – Custom Authentication – you call YouTube API track token [easiest]
Getting Started
Google Developers KeyDevelopment Environment
Samples
Google Developers Key
http://code.google.com/apis/youtube/dashboard/
Development Environment
• Accessing dev servers• Plugging in Developers Key
Getting YouTube Content
Standard FeedsDiscovering Videos
ChannelsPlaylists
Displaying Videos
Standard Feeds
http://code.google.com/apis/youtube/2.0/reference.html#Standard_feeds
• Top Rated• Top Favorites• Most Viewed• Most Popular• Most Recent• Etc…• Filter by Time• Filter by Region• Filter by Category
Discovering Videos
• Keywords• Categories• Categories/Keywords• Users
Categorieshttp://gdata.youtube.com/schemas/2007/categories.cat
<app:categories [namespaces]> <atom:category term="Film" label="Film & Animation" xml:lang="en-US"> <yt:assignable/> <yt:browsable regions="AU BR CA CZ DE ES FR GB HK IE IL IN IT JP KR MX NL NZ PL PT RU SE TW US"/> </atom:category></app>
Video by Keyword/Tag
http://gdata.youtube.com/feeds/api/videos/
• Append /-/keyword/ to video feed URL• Use query string ?category=keyword• Not (-) Or (|)– /-/Kennedy/-Bobby/-Ted/-Patrick– /-/Kennedy/Bobby|Ted|Patrick
Video by Category
http://gdata.youtube.com/feeds/api/videos/
• Append /-/Category/ to video feed URL• Use query string ?category=Category• Multiple Categories– /-/Comedy/Music/Shortmov/– ?category=Comedy,Music,Shortmov
• Category ARE case sensitive
Channelshttp://gdata.youtube.com/feeds/api/channels?v=2
• Search just like videos using “?q=keywords/tags”
<feed [namespaces>… metadata …<openSearch:totalResults /><openSearch:startIndex /><openSearch: itemsPerPage /><entry>
<title /><summary /><gd:feedlink url=“…” hitcount=“…”>
</entry></feed>
Playlists
http://gdata.youtube.com/feeds/api/playlists/snippets?v=2
• Searchjust like a Channel<feed [namespaces>
… metadata …<openSearch:totalResults /><openSearch:startIndex /><openSearch: itemsPerPage /><entry>
<title /><summary /><content src=“…”><yt:countHint /><yt:playlistId />
</entry></feed>
Displaying Videos
• JavaScript API• Flash API
Interacting with YouTube Content
AuthenticationComments
RatingsUpdatingUploading
ClientLogin
https://www.google.com/youtube/accounts/ClientLogin
• Pass in:– Email– Passwd
• Receive: Auth=AIwbFARksypDdUSGGYRI_5v7Z9TaijoPQqpIfCEjTFPAikn_---OC-I1VJtQ
YouTubeUser=testuser• Send into all requests:
Header:Authorization = “GoogleLogin auth=
AIwbFARksypDdUSGGYRI_5v7Z9TaijoPQqpIfCEjTFPAikn_---OC-I1VJtQ”
Comments
• Get Video Feed• Look for:
<gd:comments><gd:feedlink href=“…” countHint=“…” />
</gd:comments>• Pass in
Ratings
• Link embeded in the video feed<link
rel='http://gdata.youtube.com/schemas/2007#video.ratings' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/ratings'/>
• Send back to this URL:<entry [namespaces]>
<gd:rating value="4" min="1" max="5"/></entry>
Updating• Link embeded in the video feed<link rel='edit' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/users/andyland74/uploads/914IMX9RSTE'>
• Send Back to this URL:<entry [namespaces]>
<media:group><media:title type=“…“ /><media:description type=“…“ /><media:category scheme=“…“ /><media:keywords />
</media:group> </entry>
Uploading
http://uploads.gdata.youtube.com/feeds/api/users
• Include in Header– Authentication– Slug (filename)
• Include in Body– Video File<entry [namespace]>
<media:group> <media:title type=”...” />
<media:description type=“…“ /><media:keywords /><media:category scheme=“…“ />
</media:group></entry>
Challenges
• Documentation is poor• No true congruity in related links• Error messages are non-descriptive
Developer Resources
• Google Group– http://groups.google.com/group/youtube-api-
gdata• Google Code Help– http://code.google.com/support/bin/topic.py?
topic=12357
Contact
• Ron West• @notronwest• facebook.com/notronwest• www.notronwest.com/blog