API: Application Programming Interface: (Giao diện lập trình ứng dụng): là 1 dạng thể hiện, giúp phát triển ứng dụng 1 cách dễ dàng qua việc hỗ trợ rất nhiều những hoạt động dịch vụ giúp cho người dùng có thể tương tác với hệ thống. Việc tương tác đó có thể được định nghĩa qua các hàm, các tham số, các biến có liên quan đến hệ thống chính.
Trang 1Báo cáo đề tài tìm hiểu
Web API -Youtube API
N-L-S
Báo cáo giới thiệu sơ bộ về web api, cách sử dụng chung cho một web api và cuối cùng là giới thiệu một web api cụ thể (Youtube API).
Trang 2Thành viên:
− Nguyễn Quốc Nhã – 0912317
− Huỳnh Văn Lộc - 0912268
− Châu Hoàn Sơn – 0912392
Mục lục:
1. Khái niệm 3
2. Tương tác 3
3. Tại sao sử dụng 3
4. Hỗ trợ - đóng gói 4
Trang 3II. Cách sử dụng chung
1. Đối với Web API dạng Link 4
2. Đối với WEB API dạng SDK 4
3. Đối với dạng Thư viện Java Script 5
III. Youtube API 1. Giới thiệu chung 6
2. Một số api mà youtube hỗ trợ (v2/.Net) 1. Xác thực tài khoản 7
2. Video feed và entry 7
3. Truy xuất và tìm kiếm video 10
4. Upload video 14
5. Cập nhật và xoá video 15
6. Sử dụng tính năng cộng đồng 16
7. Lưu trữ và thu thập video 17
8. Kích hoạt tương tác người dùng 19
9. Xử lý theo lô 21
Trang 4I. Web API:
1. Khái niệm:
− API: Application Programming Interface: (Giao diện lập trình ứng dụng): là 1 dạng thể hiện, giúp phát triển ứng dụng 1 cách dễ dàng qua việc hỗ trợ rất nhiều những hoạt động / dịch vụ giúp cho người dùng có thể tương tác với hệ thống Việc tương tác đó có thể được định nghĩa qua các hàm, các tham số, các biến có liên quan đến hệ thống chính
VD: Window có các API để tương tác với các service của hệ thống, các phần mềm mặc định
− Web API: Tương tự như API, WEB API cung cấp cho người dùng 1 dạng các thể hiện, giúp cho người dùng có thể tương tác được với dịch vụ được cung cấp trên web Người dùng thông qua API để sử dụng các dịnh vụ đó
VD: các hàm, các giao thức, gói tin gửi đi, các URL,
2. Tương tác:
WEB API là hệ thống thiết lập các thông điệp dạng HTTP, NET/TCP, để gửi lên web, sau đó nhận các thông điệp phản hồi về (thường là dạng JSON, XML có cấu trúc rõ ràng)
3. Tại sao sử dụng:
− Nguyên nhân hình thành các dịch vụ web:
• Hệ thống Internet ngày càng phát triển, phần mềm sử dụng hệ thống internet càng nhiều
• 1 số phần mềm đòi hỏi về lượng thông tin lớn, dữ liệu lớn, nên không thể lưu trực tiếp tại thiết bị sử dụng
• 1 số loại xử lý cần được cập nhật realtime (theo thời gian thực) để đảm bảo sự đúng đắn của thông tin (chứng khoán, tiền tệ, giá cả, ngân hàng, )
• 1 số phần mềm đòi hỏi xử lý nhanh và mạnh, mà các thiết bị lại không thể thực hiện được do cấu hình không đủ
• Các dịch vụ mạng xã hội, cần sự giao tiếp với rất nhiều người cùng lúc
− Thông thường, để sử dụng các dịch vụ đó thì người dùng cần dùng trình duyệt, truy cập web và thực hiện.Nhưng họ chỉ có thể sử dụng các giao diện mà nhà cung cấp họ thiết kế sẵn, có thể rườm rà, rắc rối Lúc đó thì họ lại nghĩ đến việc tự viết 1 ứng dung, có thể thực hiện các thao tác như giao diện trên web đó, nhưng được tùy chỉnh cho dễ sử dụng hơn, giao diện đẹp hơn, chạy nhanh hơn các trình duyệt web (tránh load những phần thừa), thì họ phải sử dụng những dịch vụ riêng biệt để tương tác với hệ thống cung cấp các dịch vụ trên, nói cách khác là phải sử dụng Web API
Đó là nguyên nhân chính dẫn đến việc sử dụng web API
4. Hỗ trợ - Đóng gói:
WEB API thường được cung cấp dưới các dạng sau:
Trang 5− Địa chỉ web của API.Mỗi API sẽ có địa chỉ web tương ứng Ta truy vấn các địa chỉ này và các tham số truyền vào để sử dụng.
VD: Google map: http://maps.googleapis.com/maps/api/
Google Translator: http://translate.googleapis.com/translate_static
Yahoo! Map Service: http://local.yahooapis.com/MapsService/
− Các thư viện động: Web API được đóng gói trong 1 bộ thư viện Khi sử dụng thì ta gọi các hàm trong đó để sử dung, tương tác với dịch vụ ở trên Net.
VD: Facebook SDK, Youtube Data API
− Các thư viện Javascript để dùng cho việc sử dụng Web API nhúng vào trong website của người dùng (có dạng gần giống với cách sử dụng địa chỉ web API)
VD: Google, Youtube, Youtube Player API,
II. Cách sử dụng chung:
1. Đối với Web API dạng Link:
− Ta sử dụng các http request để thực hiện việc truy vấn đến web service và nhận dữ liệu
− Các http Request là các link tương ứng từng dịch vụ mà hệ thống đó hỗ trợ Ta truyền vào các tham số, sau đó dùng nó để truy vấn lên hệ thống trên Web Hệ thống sẽ xử lý
và trả về kết quả
VD: đoạn code sau sẽ truy vấn lên Yahoo API, tìm và lấy “Tên” của tỉnh ở tọa độ Kinh độ, Vĩ độ được truyền vào Kết quả trả về là 1 file Json, trong đó có tên của tỉnh cần lấy
publicWebClient webClient;
Uri uri = newUri("http://where.yahooapis.com/geocode?&gflags=R&location="
+ [kinh độ] + "+" + [vĩ độ]);
webClient.DownloadStringAsync(uri);
2. Đối với WEB API dạng SDK:
− Ta download và cài đặt các SDK đó vào máy
− Add reference file dll vao project sau đó sử dụng các đối tượng và phương thức cung cấp để thực hiện các thao tác cần thiết
VD: Đoạn code sau dùng để tìm kiếm video trên youtube:
Trang 6query.SafeSearch=YouTubeQuery.SafeSearchValues.None;
Feed<Video> videoFeed = request.Get<Video>(query);
printVideoFeed(videoFeed);
3. Đối với dạng Thư viện Java Script:
− Ta sử dụng chính các link đến Web API đó để lấy thông tin, khởi tạo các giá trị cho đối tượng và thực hiện
VD: đoạn code sau thực hiện việc hiên thị giao diện của Google map, hiển thị map và các phím control trên trình duyệt Trong đây, ta sử dụng link đến Google Map API để lấy file javascript, sử dụng nó để hiển thị bản đồ với các thông số tương ứng
Trang 7III. Youtube API
1. Giới thiệu chung
− Như chúng ta đã biết, Youtube hiện hay đang là website chia sẻ video hàng đầu của thế giới.Số lượt sử dụng hàng ngày của Youtube đến hơn 200.000 lượt (thống kê 2010) Người dùng Youtube có thể đăng tải các video, comment, lưu video vào danh sách ưa thích, tạo page riêng, và nhiều ứng dụng khác nữa
− Để giúp cho Youtube ngày càng phát triển, họ đã đưa ra bộ công cụ, giúp cho người dùng có thể dễ dàng thực hiện các thao tác đã nêu trên 1 cách dễ dàng trên ứng dụng riêng của họ, đó chính là Youtube API
− Để sử dụng Youtube API, người dùng chỉ cần sử dụng các thẻ HTMl và Javascript là
có thể đưa các đoạn Video, các thao tác Comment hay Upload và chính ứng dụng web của mình
− Youtube Api hiện nay cung cấp cho người dùng 2 dạng: Data API và Player API.
1. Data API
• Data API cho phép người dùng có thể thực hiện nhiều thao tác tương tự trên website Youtube: Upload video, Sửa đổi danh sách ưa thích, tìm kiếm, comment, đánh giá,
2. Player API:
• Player API là 1 dạng API cho phép người dùng nhúng đoạn video vào trong trang web của họ
Trang 8• Người dùng có thể xem Video, có thể thực hiện các thao tác Play, Pause, Stop, tăng giảm âm lượng, trực tiếp từ web của họ mà không phải thông qua Youtube
2. Một số api mà youtube hỗ trợ (v2/.Net)
1. Xác thực tài khoản
DeveloperKey: http://code.google.com/apis/youtube/dashboard
YouTubeRequestSettings settings =
newYouTubeRequestSettings("example app", developerKey);
YouTubeRequest request =newYouTubeRequest(settings);
1. Video feed và entry
1.1 Hiện video feed
Feed<Video> videoFeed = request.Get<Video>(new Uri(feedUrl));
Trang 91.2 Truy xuất video entry qua id
Uri videoEntryUrl=
newUri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");
Video video = request.Retrieve<Video>(videoEntryUrl);
printVideoEntry(videoEntry);
1.3 Nội dung của video entry
staticvoid printVideoEntry(Video video)
Trang 10Console.WriteLine("Video is not live.");
string stateName = video.Status.Name;
Trang 11Console.WriteLine("\tThumbnail time index: "+ thumbnail.Time);
}
Console.WriteLine("Media:");
foreach(Google.GData.YouTube.MediaContent mediaContent in video.Contents) {
Console.WriteLine("\tMedia Location: "+ mediaContent.Url);
Console.WriteLine("\tMedia Type: "+ mediaContent.Format);
Console.WriteLine("\tDuration: "+ mediaContent.Duration);
}
}
2. Truy xuất và tìm kiếm video
2.1 Truy xuất feed theo chuẩn
*Lấy feed theo feed id
http://gdata.youtube.com/feeds/api/standardfeeds/FEED_IDENTIFIER
Feed Name Feed Identifier
Most Viewed most_viewed
Top Rated top_rated
Recently Featured recently_featured
Watch On Mobile watch_on_mobile
Most Discussed most_discussed
Top Favorites top_favorites
Most Responded most_responded
Most Recent most_recent
*Lấy feed theo tên vùng và loại:
http://gdata.youtube.com/feeds/api/standardfeeds/localeID/
feedID_CATEGORY_NAME
vd: http://gdata.youtube.com/feeds/api/standardfeeds/JP/top_rated_Comedy
Trang 12(Standard:mostpopular)Feed<Video> videoFeed =request.GetStandardFeed(YouTubeQuery.MostPopular);printVideoFeed(videoFeed);
2.2 Truy xuất video feed upload bởi người dùng
Trang 13Uri videoEntryUrl =
newUri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");
Video video = request.Retrieve<Video>(videoEntryUrl);
Feed<Video> relatedVideos = request.GetRelatedVideos(video);
printVideoFeed(relatedVideos)
2.4 Tìm kiếm video
(từ khoá: puppy)
YouTubeQuery query =newYouTubeQuery(YouTubeQuery.DefaultVideoUri);
//order results by the number of views (most viewed first)
query.OrderBy="viewCount";
// search for puppies and include restricted content in the search results
// query.SafeSearch could also be set to YouTubeQuery.SafeSearchValues.Moderatequery.Query="puppy";
Trang 14*Tìm kiếm video sử dụng category và keyword
(loại: news, sports, football)
YouTubeQuery query =newYouTubeQuery(YouTubeQuery.DefaultVideoUri);
query.Categories.Add(newQueryCategory(category3));
Trang 15Feed<Video> videoFeed = request.Get<Video>(query);
printVideoFeed(videoFeed);
*Tìm kiếm video sử dụng developer tag
YouTubeQuery query =newYouTubeQuery(YouTubeQuery.DefaultVideoUri);
AtomCategory category
=newAtomCategory("uploaded_on_example.com",YouTubeNameTable.DeveloperTagSchema);
Video newVideo =newVideo();
newVideo.Title="My Test Movie";
newVideo.Tags.Add(newMediaCategory("Autos",YouTubeNameTable.CategorySchema));
// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
newVideo.YouTubeEntry.MediaSource=newMediaFileSource("c:\\file.mov",
"video/quicktime");
Video createdVideo = request.Upload(newVideo);
3.2 Upload dựa vào trình duyệt
Video newVideo =newVideo();
newVideo.Title="My Test Movie";
newVideo.Tags.Add(newMediaCategory("Autos",YouTubeNameTable.CategorySchema));
newVideo.Keywords="cars, funny";
Trang 16// alternatively, you could just specify a descriptive string
// newVideo.YouTubeEntry.setYouTubeExtension("location", "Mountain View, CA");
FormUploadToken token = request.CreateFormUploadToken(newVideo);
Console.WriteLine("Video is not live.");
string stateName = video.Status.Name;
Trang 17*Truy xuất video comment
Feed comments = request.GetComments(v);
foreach (Comment c in comments.Entries)
{
Console.WriteLine(c.Content);
}
*Thêm comment
Comment c = new Comment();
c.Content = "This is my comment.";
request.AddComment(video, c);
// retrieve the comments for the video
Feed fc = request.GetComments(video);
List comments = new List(fc.Entries);
// create a reply to the third comment
Video video = request.Retrieve<Video>(videoEntryUrl);
Feed<Video> responses = request.GetResponseVideos(video);
printVideoFeed(responses);
*Thêm video response
request.Insert(video.ResponseUri, responseVideo);
Trang 18*Truy xuất video yêu thích của người dùng
Feed<Video> feed = request.GetFavoriteVideo(username);
printVideoFeed(videoFeed);
*Thêm video yêu thích
string videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0";YouTubeEntry videoEntry = (YouTubeEntry) service.Get(videoEntryUrl);
service.Insert(new Uri(feedUrl), videoEntry);
*Xoá video yêu thích
request.Delete(favoriteVideo);
6.2 Playlist
*Truy xuất playlist của người dùng
Feed<Playlist> userPlaylists =request.GetPlaylistsFeed(username);
*Truy xuất thông tin playlist
Feed<PlayListMember> list = request.GetPlaylist(p);
foreach (Video v in list.Entries)
{
Trang 19printVideoEntry(v);
}
*Thêm playlist
Playlist pl =newPlaylist();
pl.Title="this is the title";
pl.Summary="this is my new playlist";
Playlist createdPlaylist = request.Insert(
newUri("http://gdata.youtube.com/feeds/api/users/default/playlists"), pl);
*Cập nhật playlist
pl.Summary = "updated summary";
request.Update(pl);
+Thêm video vào playlist
// For Playlist object p
PlayListMember pm =newPlayListMember();
// Insert <id> or <videoid> for video here
pm.Id= VIDEOID;
request.AddToPlaylist(p, pm);
+Sửa thông tin của playlist
// move PlayListMember pm to the top of the playlist
YouTubeQuery query = new YouTubeQuery(feedUrl);
SubscriptionFeed subFeed = service.GetSubscriptions(query);
foreach (SubscriptionEntry entry in subFeed.Entries)
{
Console.WriteLine("Title: " + entry.Title.Text);
Trang 20*Truy xuất profile của người dùng
string feedUrl ="http://gdata.youtube.com/feeds/api/users/GoogleDevelopers";
ProfileEntry profile =(ProfileEntry) service.Get(feedUrl);
Trang 21Console.WriteLine("Subscriber count: "+ profile.Statistics.SubscriberCount);
Console.WriteLine("View count: "+ profile.Statistics.ViewCount);
Console.WriteLine("Watch count: "+ profile.Statistics.WatchCount);
Console.WriteLine("Last web access: "+ profile.Statistics.LastWebAccess);
7.2 Contacts
*Truy xuất contact của người dùng
string feedUrl ="http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/contacts";YouTubeQuery query =newYouTubeQuery(feedUrl);
FriendsFeed friendFeed = service.GetFriends(query);
foreach(FriendsEntry entry in friendFeed.Entries)
string feedUrl ="http://gdata.youtube.com/feeds/api/users/default/contacts";
FriendsEntry newFriend =newFriendsEntry();
Trang 227.3 Messages
*Truy xuất tin nhắn
string feedUrl ="http://gdata.youtube.com/feeds/api/users/default/inbox";
YouTubeQuery query =newYouTubeQuery(feedUrl);
MessageFeed messagesFeed = service.GetMessages(query);
foreach(MessageEntry entry in messagesFeed.Entries)
8. Xử lý theo lô (batch processing)
YouTubeRequestSettings settings =newYouTubeRequestSettings("NETUnittests",
this.ytClient,this.ytDevKey,this.ytUser,this.ytPwd);
YouTubeRequest request =newYouTubeRequest(settings);
// Retrieve the user's playlists feed and create a list of playlists
Feed<Playlist> playlistsFeed = request.GetPlaylistsFeed(null);
List<Playlist> playlists =newList(playlistsFeed.Entries);
// Retrieve a list of the videos in the third playlist in the feed
// (You could choose another playlist.)
Feed<PlayListMember> playListMembersFeed =
request.GetPlaylist(playlists[2]);
Trang 23List<PlayListMember> playListMembers =
newList<PlayListMember>(playlistsMembersFeed.Entries);
List<PlayListMember> batch =newList<PlayListMember>();
// Delete the third item in the list
// Add a video to the playlist
PlayListMember toBatch =newPlayListMember();
// Set videoid to the videoid of video being added to the playlist